blob: 3b789f689c7eef26702fc093cfa0fc50b5fc2d0b [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)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080053 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Jeongik Cha538c0d02019-07-11 15:54:27 +090056func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
57 if sc, ok := ctx.Module().(sdkContext); ok {
58 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
59 if usePlatformAPI != (sc.sdkVersion() == "") {
60 if usePlatformAPI {
61 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
62 } else {
63 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
64 }
65 }
66
67 }
68}
69
Colin Cross2fe66872015-03-30 17:20:39 -070070// TODO:
71// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070072// Renderscript
73// Post-jar passes:
74// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070075// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070076// DroidDoc
77// Findbugs
78
Colin Cross89536d42017-07-07 14:35:50 -070079type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
81 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070083
84 // list of source files that should not be used to build the Java module.
85 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080086 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070089 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross86a63ff2017-09-27 17:33:10 -070091 // list of directories that should be excluded from java_resource_dirs
92 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070093
Colin Cross0f37af02017-09-27 17:42:05 -070094 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080095 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070096
Colin Crosscedd4762018-09-13 11:26:19 -070097 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070099
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 // list of module-specific flags that will be used for javac compiles
101 Javacflags []string `android:"arch_variant"`
102
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200103 // list of module-specific flags that will be used for kotlinc compiles
104 Kotlincflags []string `android:"arch_variant"`
105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700107 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700108
109 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700110 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700111
112 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700114
Colin Cross540eff82017-06-22 17:01:52 -0700115 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700117
118 // If not blank, set the java version passed to javac as -source and -target
119 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700120
Colin Cross9ae1b922018-06-26 17:59:05 -0700121 // If set to true, allow this module to be dexed and installed on devices. Has no
122 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700123 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700124
Colin Cross0f37af02017-09-27 17:42:05 -0700125 // If set to true, include sources used to compile the module in to the final jar
126 Include_srcs *bool
127
Vladimir Marko0975ee02019-04-02 10:29:55 +0100128 // If not empty, classes are restricted to the specified packages and their sub-packages.
129 // This restriction is checked after applying jarjar rules and including static libs.
130 Permitted_packages []string
131
Colin Crossbe9cdb82019-01-21 21:37:16 -0800132 // List of modules to use as annotation processors
133 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700134
Nan Zhang61eaedb2017-11-02 13:28:15 -0700135 // The number of Java source entries each Javac instance can process
136 Javac_shard_size *int64
137
Nan Zhang5f8cb422018-02-06 10:34:32 -0800138 // Add host jdk tools.jar to bootclasspath
139 Use_tools_jar *bool
140
Colin Cross1369cdb2017-09-29 17:58:17 -0700141 Openjdk9 struct {
142 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700144
145 // List of javac flags that should only be used when passing -source 1.9
146 Javacflags []string
147 }
Colin Crosscb933592017-11-22 13:49:43 -0800148
Colin Cross81440082018-08-15 20:21:55 -0700149 // When compiling language level 9+ .java code in packages that are part of
150 // a system module, patch_module names the module that your sources and
151 // dependencies should be patched into. The Android runtime currently
152 // doesn't implement the JEP 261 module system so this option is only
153 // supported at compile time. It should only be needed to compile tests in
154 // packages that exist in libcore and which are inconvenient to move
155 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100156 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700157
Colin Crosscb933592017-11-22 13:49:43 -0800158 Jacoco struct {
159 // List of classes to include for instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. If unset defaults to all
161 // classes.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Include_filter []string
166
167 // List of classes to exclude from instrumentation with jacoco to collect coverage
168 // information at runtime when building with coverage enabled. Overrides classes selected
169 // by the include_filter property.
170 // Supports '*' as the last character of an entry in the list as a wildcard match.
171 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
172 // it matches classes in the package that have the class name as a prefix.
173 Exclude_filter []string
174 }
175
Andreas Gampef3e5b552018-01-22 21:27:21 -0800176 Errorprone struct {
177 // List of javac flags that should only be used when running errorprone.
178 Javacflags []string
179 }
180
Colin Cross0f2ee152017-12-14 15:22:43 -0800181 Proto struct {
182 // List of extra options that will be passed to the proto generator.
183 Output_params []string
184 }
185
Colin Crosscb933592017-11-22 13:49:43 -0800186 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800187
188 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800189 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700190}
191
Colin Cross89536d42017-07-07 14:35:50 -0700192type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700193 // list of module-specific flags that will be used for dex compiles
194 Dxflags []string `android:"arch_variant"`
195
Jeongik Cha538c0d02019-07-11 15:54:27 +0900196 // if not blank, set to the version of the sdk to compile against.
197 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800198 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Cross83bb3162018-06-25 15:48:06 -0700200 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
201 // Defaults to sdk_version if not set.
202 Min_sdk_version *string
203
Dan Willemsen419290a2018-10-31 15:28:47 -0700204 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
205 // Defaults to sdk_version if not set.
206 Target_sdk_version *string
207
Jeongik Cha538c0d02019-07-11 15:54:27 +0900208 // It must be true only if sdk_version is empty.
209 // This field works in only android_app, otherwise nothing happens.
Colin Cross6af2e492018-05-22 11:12:33 -0700210 Platform_apis *bool
211
Colin Crossebe1a512017-11-14 13:12:14 -0800212 Aidl struct {
213 // Top level directories to pass to aidl tool
214 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700215
Colin Crossebe1a512017-11-14 13:12:14 -0800216 // Directories rooted at the Android.bp file to pass to aidl tool
217 Local_include_dirs []string
218
219 // directories that should be added as include directories for any aidl sources of modules
220 // that depend on this module, as well as to aidl for this module.
221 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100222
223 // whether to generate traces (for systrace) for this interface
224 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100225
226 // whether to generate Binder#GetTransaction name method.
227 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800228 }
Colin Cross92430102017-10-09 14:59:32 -0700229
230 // If true, export a copy of the module as a -hostdex module for host testing.
231 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700232
Colin Cross7f87f4f2019-04-24 13:41:45 -0700233 Target struct {
234 Hostdex struct {
235 // Additional required dependencies to add to -hostdex modules.
236 Required []string
237 }
238 }
239
David Brazdil17ef5632018-06-27 10:27:45 +0100240 // If set to true, compile dex regardless of installable. Defaults to false.
241 Compile_dex *bool
242
Colin Cross66dbc0b2017-12-28 12:23:20 -0800243 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700244 // If false, disable all optimization. Defaults to true for android_app and android_test
245 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800246 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700247 // True if the module containing this has it set by default.
248 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800249
250 // If true, optimize for size by removing unused code. Defaults to true for apps,
251 // false for libraries and tests.
252 Shrink *bool
253
254 // If true, optimize bytecode. Defaults to false.
255 Optimize *bool
256
257 // If true, obfuscate bytecode. Defaults to false.
258 Obfuscate *bool
259
260 // If true, do not use the flag files generated by aapt that automatically keep
261 // classes referenced by the app manifest. Defaults to false.
262 No_aapt_flags *bool
263
264 // Flags to pass to proguard.
265 Proguard_flags []string
266
267 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800268 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800269 }
270
Colin Cross1369cdb2017-09-29 17:58:17 -0700271 // When targeting 1.9, override the modules to use with --system
272 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700273
274 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800275 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700276}
277
Sasha Smundak2057f822019-04-16 17:16:58 -0700278func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
279 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
280}
281
Colin Cross46c9b8b2017-06-22 16:51:17 -0700282// Module contains the properties and members used by all java module types
283type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700284 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700285 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900286 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700287
Colin Cross89536d42017-07-07 14:35:50 -0700288 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700289 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700290 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700291
Colin Cross331a1212018-08-15 20:40:52 -0700292 // jar file containing header classes including static library dependencies, suitable for
293 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700294 headerJarFile android.Path
295
Colin Cross331a1212018-08-15 20:40:52 -0700296 // jar file containing implementation classes including static library dependencies but no
297 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700298 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700299
Colin Cross331a1212018-08-15 20:40:52 -0700300 // jar file containing only resources including from static library dependencies
301 resourceJar android.Path
302
Colin Cross0c4ce212019-05-03 15:28:19 -0700303 // args and dependencies to package source files into a srcjar
304 srcJarArgs []string
305 srcJarDeps android.Paths
306
Colin Cross331a1212018-08-15 20:40:52 -0700307 // jar file containing implementation classes and resources including static library
308 // dependencies
309 implementationAndResourcesJar android.Path
310
311 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700312 dexJarFile android.Path
313
Colin Cross43f08db2018-11-12 10:13:39 -0800314 // output file that contains classes.dex if it should be in the output file
315 maybeStrippedDexJarFile android.Path
316
Colin Crosscb933592017-11-22 13:49:43 -0800317 // output file containing uninstrumented classes that will be instrumented by jacoco
318 jacocoReportClassesFile android.Path
319
Colin Cross66dbc0b2017-12-28 12:23:20 -0800320 // output file containing mapping of obfuscated names
321 proguardDictionary android.Path
322
Colin Cross331a1212018-08-15 20:40:52 -0700323 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700324 outputFile android.Path
325 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700326
Colin Cross635c3b02016-05-18 15:37:25 -0700327 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700328
Colin Cross635c3b02016-05-18 15:37:25 -0700329 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700330
Colin Cross2fe66872015-03-30 17:20:39 -0700331 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700332 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800333
334 // list of .java files and srcjars that was passed to javac
335 compiledJavaSrcs android.Paths
336 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800337
338 // list of extra progurad flag files
339 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900340
Colin Cross094054a2018-10-17 15:10:48 -0700341 // manifest file to use instead of properties.Manifest
342 overrideManifest android.OptionalPath
343
Jiyong Park1be96912018-05-28 18:02:19 +0900344 // list of SDK lib names that this java moudule is exporting
345 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700346
347 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
348 // filter out Exclude_srcs, will be used by android.IDEInfo struct
349 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800350
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800351 // expanded Jarjar_rules
352 expandJarjarRules android.Path
353
Vladimir Marko0975ee02019-04-02 10:29:55 +0100354 // list of additional targets for checkbuild
355 additionalCheckedModules android.Paths
356
Colin Cross988708c2019-05-06 14:04:11 -0700357 // Extra files generated by the module type to be added as java resources.
358 extraResources android.Paths
359
Colin Crossf24a22a2019-01-31 14:12:44 -0800360 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800361 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800362
363 // list of the xref extraction files
364 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700365}
366
Colin Cross41955e82019-05-29 14:40:35 -0700367func (j *Module) OutputFiles(tag string) (android.Paths, error) {
368 switch tag {
369 case "":
370 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700371 case ".jar":
372 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700373 case ".proguard_map":
374 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700375 default:
376 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
377 }
Colin Cross54250902017-12-05 09:28:08 -0800378}
379
Jiyong Park8fd61922018-11-08 02:50:25 +0900380func (j *Module) DexJarFile() android.Path {
381 return j.dexJarFile
382}
383
Colin Cross41955e82019-05-29 14:40:35 -0700384var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800385
Colin Crossf506d872017-07-19 15:53:04 -0700386type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700387 HeaderJars() android.Paths
388 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700389 ResourceJars() android.Paths
390 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800391 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700392 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900393 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700394 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700395}
396
Jiyong Parkc678ad32018-04-10 13:07:10 +0900397type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700398 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
399 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400}
401
Nan Zhangb2b33de2018-02-23 11:18:47 -0800402type SrcDependency interface {
403 CompiledSrcs() android.Paths
404 CompiledSrcJars() android.Paths
405}
406
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800407type xref interface {
408 XrefJavaFiles() android.Paths
409}
410
Nan Zhangb2b33de2018-02-23 11:18:47 -0800411func (j *Module) CompiledSrcs() android.Paths {
412 return j.compiledJavaSrcs
413}
414
415func (j *Module) CompiledSrcJars() android.Paths {
416 return j.compiledSrcJars
417}
418
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800419func (j *Module) XrefJavaFiles() android.Paths {
420 return j.kytheFiles
421}
422
Nan Zhangb2b33de2018-02-23 11:18:47 -0800423var _ SrcDependency = (*Module)(nil)
424
Colin Cross89536d42017-07-07 14:35:50 -0700425func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
426 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
427 android.InitDefaultableModule(module)
428}
429
Colin Crossbe1da472017-07-07 15:59:46 -0700430type dependencyTag struct {
431 blueprint.BaseDependencyTag
432 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700433}
434
Colin Crossa4f08812018-10-02 22:03:40 -0700435type jniDependencyTag struct {
436 blueprint.BaseDependencyTag
437 target android.Target
438}
439
Colin Crossbe1da472017-07-07 15:59:46 -0700440var (
Colin Cross4b964c02018-10-15 16:18:06 -0700441 staticLibTag = dependencyTag{name: "staticlib"}
442 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800443 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700444 bootClasspathTag = dependencyTag{name: "bootclasspath"}
445 systemModulesTag = dependencyTag{name: "system modules"}
446 frameworkResTag = dependencyTag{name: "framework-res"}
447 frameworkApkTag = dependencyTag{name: "framework-apk"}
448 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800449 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700450 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
451 certificateTag = dependencyTag{name: "certificate"}
452 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700453 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700454)
Colin Cross2fe66872015-03-30 17:20:39 -0700455
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900456func defaultSdkVersion(ctx checkVendorModuleContext) string {
457 if ctx.SocSpecific() || ctx.DeviceSpecific() {
458 return "system_current"
459 }
460 return ""
461}
462
463type checkVendorModuleContext interface {
464 SocSpecific() bool
465 DeviceSpecific() bool
466}
467
Colin Crossfc3674a2017-09-18 17:41:52 -0700468type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700469 useModule, useFiles, useDefaultLibs, invalidVersion bool
470
Colin Cross86a60ae2018-05-29 14:44:55 -0700471 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700472 systemModules string
473
Colin Crossa97c5d32018-03-28 14:58:31 -0700474 frameworkResModule string
475
Colin Cross86a60ae2018-05-29 14:44:55 -0700476 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700477 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100478
479 noStandardLibs, noFrameworksLibs bool
480}
481
482func (s sdkDep) hasStandardLibs() bool {
483 return !s.noStandardLibs
484}
485
486func (s sdkDep) hasFrameworkLibs() bool {
487 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700488}
489
Colin Crossa4f08812018-10-02 22:03:40 -0700490type jniLib struct {
491 name string
492 path android.Path
493 target android.Target
494}
495
Colin Cross0ea8ba82019-06-06 14:33:29 -0700496func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800497 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
498}
499
Colin Cross0ea8ba82019-06-06 14:33:29 -0700500func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800501 return j.shouldInstrument(ctx) &&
502 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
503 ctx.Config().UnbundledBuild())
504}
505
Colin Cross83bb3162018-06-25 15:48:06 -0700506func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900507 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700508}
509
510func (j *Module) minSdkVersion() string {
511 if j.deviceProperties.Min_sdk_version != nil {
512 return *j.deviceProperties.Min_sdk_version
513 }
514 return j.sdkVersion()
515}
516
Dan Willemsen419290a2018-10-31 15:28:47 -0700517func (j *Module) targetSdkVersion() string {
518 if j.deviceProperties.Target_sdk_version != nil {
519 return *j.deviceProperties.Target_sdk_version
520 }
521 return j.sdkVersion()
522}
523
Colin Crossbe1da472017-07-07 15:59:46 -0700524func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700525 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100526 sdkDep := decodeSdkDep(ctx, sdkContext(j))
527 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700528 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100530 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100531 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700532 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700533 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700534 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100535 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700536 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700537 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700538 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
539 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800540 }
Colin Crossbe1da472017-07-07 15:59:46 -0700541 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700542 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100543 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100544 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100545 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700546 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700547 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100548 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700549 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800550 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800551 if ctx.ModuleName() == "android_stubs_current" ||
552 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700553 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700554 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800555 }
Colin Cross2fe66872015-03-30 17:20:39 -0700556 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700557
Colin Cross42d48b72018-08-29 14:10:52 -0700558 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
559 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700560
Colin Crossbe9cdb82019-01-21 21:37:16 -0800561 ctx.AddFarVariationDependencies([]blueprint.Variation{
562 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
563 }, pluginTag, j.properties.Plugins...)
564
Colin Crossfe17f6f2019-03-28 19:30:56 -0700565 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700566 if j.hasSrcExt(".proto") {
567 protoDeps(ctx, &j.protoProperties)
568 }
Colin Cross93e85952017-08-15 13:34:18 -0700569
570 if j.hasSrcExt(".kt") {
571 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
572 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700573 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
574 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800575 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800576 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
577 }
Colin Cross93e85952017-08-15 13:34:18 -0700578 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800579
580 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700581 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800582 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700583}
584
585func hasSrcExt(srcs []string, ext string) bool {
586 for _, src := range srcs {
587 if filepath.Ext(src) == ext {
588 return true
589 }
590 }
591
592 return false
593}
594
Nan Zhang61eaedb2017-11-02 13:28:15 -0700595func shardPaths(paths android.Paths, shardSize int) []android.Paths {
596 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
597 for len(paths) > shardSize {
598 ret = append(ret, paths[0:shardSize])
599 paths = paths[shardSize:]
600 }
601 if len(paths) > 0 {
602 ret = append(ret, paths)
603 }
604 return ret
605}
606
Colin Cross6af17aa2017-09-20 12:59:05 -0700607func (j *Module) hasSrcExt(ext string) bool {
608 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700609}
610
Colin Cross46c9b8b2017-06-22 16:51:17 -0700611func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700612 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700613
Colin Crossebe1a512017-11-14 13:12:14 -0800614 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
615 aidlIncludes = append(aidlIncludes,
616 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
617 aidlIncludes = append(aidlIncludes,
618 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700619
Colin Cross3047fa22019-04-18 10:56:44 -0700620 var flags []string
621 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700622
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700623 if aidlPreprocess.Valid() {
624 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700625 deps = append(deps, aidlPreprocess.Path())
626 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700627 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700628 }
629
Colin Cross3047fa22019-04-18 10:56:44 -0700630 if len(j.exportAidlIncludeDirs) > 0 {
631 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
632 }
633
634 if len(aidlIncludes) > 0 {
635 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
636 }
637
Colin Cross635c3b02016-05-18 15:37:25 -0700638 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800639 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700640 flags = append(flags, "-I"+src.String())
641 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700642
Martijn Coeneneab15642018-03-09 09:29:59 +0100643 if Bool(j.deviceProperties.Aidl.Generate_traces) {
644 flags = append(flags, "-t")
645 }
646
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100647 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
648 flags = append(flags, "--transaction_names")
649 }
650
Colin Cross3047fa22019-04-18 10:56:44 -0700651 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700652}
653
Colin Cross32f676a2017-09-06 13:41:06 -0700654type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800655 classpath classpath
656 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700657 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800658 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700659 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700660 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700661 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700662 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800663 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700664 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700665 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700666 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700667 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800668 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800669
670 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700671}
Colin Cross2fe66872015-03-30 17:20:39 -0700672
Colin Cross54250902017-12-05 09:28:08 -0800673func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
674 for _, f := range dep.Srcs() {
675 if f.Ext() != ".jar" {
676 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
677 ctx.OtherModuleName(dep.(blueprint.Module)))
678 }
679 }
680}
681
Jiyong Park2d492942018-03-05 17:44:10 +0900682type linkType int
683
684const (
685 javaCore linkType = iota
686 javaSdk
687 javaSystem
688 javaPlatform
689)
690
Jiyong Park46f78fb2018-10-20 16:33:17 +0900691func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700692 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700693 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900694 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
695 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100696 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900697 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100698 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900699 return javaCore, false
700 case name == "android_system_stubs_current":
701 return javaSystem, true
702 case strings.HasPrefix(ver, "system_"):
703 return javaSystem, false
704 case name == "android_test_stubs_current":
705 return javaSystem, true
706 case strings.HasPrefix(ver, "test_"):
707 return javaPlatform, false
708 case name == "android_stubs_current":
709 return javaSdk, true
710 case ver == "current":
711 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100712 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900713 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700714 default:
715 if _, err := strconv.Atoi(ver); err != nil {
716 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
717 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900718 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900719 }
720}
721
Jiyong Park750e5572018-01-31 00:20:13 +0900722func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700723 if ctx.Host() {
724 return
725 }
726
Jiyong Park46f78fb2018-10-20 16:33:17 +0900727 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
728 if stubs {
729 return
730 }
731 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900732 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."
733
734 switch myLinkType {
735 case javaCore:
736 if otherLinkType != javaCore {
737 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 +0900738 ctx.OtherModuleName(to))
739 }
Jiyong Park2d492942018-03-05 17:44:10 +0900740 break
741 case javaSdk:
742 if otherLinkType != javaCore && otherLinkType != javaSdk {
743 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
744 ctx.OtherModuleName(to))
745 }
746 break
747 case javaSystem:
748 if otherLinkType == javaPlatform {
749 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
750 ctx.OtherModuleName(to))
751 }
752 break
753 case javaPlatform:
754 // no restriction on link-type
755 break
Jiyong Park750e5572018-01-31 00:20:13 +0900756 }
757}
758
Colin Cross32f676a2017-09-06 13:41:06 -0700759func (j *Module) collectDeps(ctx android.ModuleContext) deps {
760 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700761
Colin Cross300f0382018-03-06 13:11:51 -0800762 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700763 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800764 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700765 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800766 } else if sdkDep.useFiles {
767 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700768 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700769 deps.aidlPreprocess = sdkDep.aidl
770 } else {
771 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800772 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700773 }
774
Colin Crossd11fcda2017-10-23 17:59:01 -0700775 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700776 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700777 tag := ctx.OtherModuleDependencyTag(module)
778
Colin Crossa4f08812018-10-02 22:03:40 -0700779 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700780 // Handled by AndroidApp.collectAppDeps
781 return
782 }
783 if tag == certificateTag {
784 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700785 return
786 }
787
Jiyong Park750e5572018-01-31 00:20:13 +0900788 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700789 switch tag {
790 case bootClasspathTag, libTag, staticLibTag:
791 checkLinkType(ctx, j, to, tag.(dependencyTag))
792 }
Jiyong Park750e5572018-01-31 00:20:13 +0900793 }
Colin Cross54250902017-12-05 09:28:08 -0800794 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800795 case SdkLibraryDependency:
796 switch tag {
797 case libTag:
798 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
799 // names of sdk libs that are directly depended are exported
800 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700801 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800802 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
803 }
Colin Cross54250902017-12-05 09:28:08 -0800804 case Dependency:
805 switch tag {
806 case bootClasspathTag:
807 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700808 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800809 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900810 // sdk lib names from dependencies are re-exported
811 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700812 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800813 case staticLibTag:
814 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
815 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
816 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700817 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900818 // sdk lib names from dependencies are re-exported
819 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700820 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800821 case pluginTag:
822 if plugin, ok := dep.(*Plugin); ok {
823 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
824 if plugin.pluginProperties.Processor_class != nil {
825 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
826 }
827 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
828 } else {
829 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
830 }
Colin Cross54250902017-12-05 09:28:08 -0800831 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100832 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800833 // framework.jar has a one-off dependency on the R.java and Manifest.java files
834 // generated by framework-res.apk
835 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
836 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800837 case frameworkApkTag:
838 if ctx.ModuleName() == "android_stubs_current" ||
839 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700840 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800841 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
842 // resource files out of there for aapt.
843 //
844 // Normally the package rule runs aapt, which includes the resource,
845 // but we're not running that in our package rule so just copy in the
846 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700847 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800848 }
Colin Cross54250902017-12-05 09:28:08 -0800849 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700850 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800851 case kotlinAnnotationsTag:
852 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800853 }
854
Colin Cross54250902017-12-05 09:28:08 -0800855 case android.SourceFileProducer:
856 switch tag {
857 case libTag:
858 checkProducesJars(ctx, dep)
859 deps.classpath = append(deps.classpath, dep.Srcs()...)
860 case staticLibTag:
861 checkProducesJars(ctx, dep)
862 deps.classpath = append(deps.classpath, dep.Srcs()...)
863 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
864 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800865 }
866 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700867 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700868 case systemModulesTag:
869 if deps.systemModules != nil {
870 panic("Found two system module dependencies")
871 }
872 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000873 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700874 panic("Missing directory for system module dependency")
875 }
Colin Crossb77043e2019-07-16 13:57:13 -0700876 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700877 }
Colin Crossec7a0422017-07-07 14:47:12 -0700878 }
Colin Cross2fe66872015-03-30 17:20:39 -0700879 })
880
Jiyong Park1be96912018-05-28 18:02:19 +0900881 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
882
Colin Cross32f676a2017-09-06 13:41:06 -0700883 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700884}
885
Colin Cross83bb3162018-06-25 15:48:06 -0700886func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700887 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800888 v := sdkContext.sdkVersion()
889 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100890 if ctx.Config().IsPdkBuild() &&
891 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700892 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800893 latestSdkVersion := 0
894 if len(sdkVersions) > 0 {
895 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
896 }
897 v = strconv.Itoa(latestSdkVersion)
898 }
899
900 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700901 if err != nil {
902 ctx.PropertyErrorf("sdk_version", "%s", err)
903 }
Nan Zhang357466b2018-04-17 17:38:36 -0700904 if javaVersion != "" {
905 ret = javaVersion
906 } else if ctx.Device() && sdk <= 23 {
907 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100908 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700909 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100910 } else if ctx.Device() &&
911 sdkContext.sdkVersion() != "" &&
912 sdkContext.sdkVersion() != "none" &&
913 sdkContext.sdkVersion() != "core_platform" &&
914 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700915 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
916 ret = "1.8"
917 } else {
918 ret = "1.9"
919 }
920
921 return ret
922}
923
Nan Zhanged19fc32017-10-19 13:06:22 -0700924func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700925
Colin Crossf03c82b2015-04-13 13:53:40 -0700926 var flags javaBuilderFlags
927
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100928 // javaVersion flag.
929 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
930
Nan Zhanged19fc32017-10-19 13:06:22 -0700931 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700932 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100933 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700934 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700935 }
Colin Cross6510f912017-11-29 00:27:14 -0800936 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700937 // Override the -g flag passed globally to remove local variable debug info to reduce
938 // disk and memory usage.
939 javacFlags = append(javacFlags, "-g:source,lines")
940 }
Colin Cross64162712017-08-08 13:17:59 -0700941
Colin Cross66548102018-06-19 22:47:35 -0700942 if ctx.Config().RunErrorProne() {
943 if config.ErrorProneClasspath == nil {
944 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
945 }
946
947 errorProneFlags := []string{
948 "-Xplugin:ErrorProne",
949 "${config.ErrorProneChecks}",
950 }
951 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
952
953 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
954 "'" + strings.Join(errorProneFlags, " ") + "'"
955 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800956 }
957
Nan Zhanged19fc32017-10-19 13:06:22 -0700958 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800959 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
960 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700961 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800962
Colin Crossbe9cdb82019-01-21 21:37:16 -0800963 flags.processor = strings.Join(deps.processorClasses, ",")
964
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100965 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100966 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800967 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
968 // Give host-side tools a version of OpenJDK's standard libraries
969 // close to what they're targeting. As of Dec 2017, AOSP is only
970 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
971 //
972 // When building with OpenJDK 8, the following should have no
973 // effect since those jars would be available by default.
974 //
975 // When building with OpenJDK 9 but targeting a version < 1.8,
976 // putting them on the bootclasspath means that:
977 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
978 // b) references to existing APIs are not reinterpreted in an
979 // OpenJDK 9-specific way, eg. calls to subclasses of
980 // java.nio.Buffer as in http://b/70862583
981 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
982 flags.bootClasspath = append(flags.bootClasspath,
983 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
984 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800985 if Bool(j.properties.Use_tools_jar) {
986 flags.bootClasspath = append(flags.bootClasspath,
987 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
988 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800989 }
990
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100991 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800992 // Manually specify build directory in case it is not under the repo root.
993 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
994 // just adding a symlink under the root doesn't help.)
995 patchPaths := ".:" + ctx.Config().BuildDir()
996 classPath := flags.classpath.FormJavaClassPath("")
997 if classPath != "" {
998 patchPaths += ":" + classPath
999 }
1000 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001001 }
1002
Nan Zhanged19fc32017-10-19 13:06:22 -07001003 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001004 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001005
Nan Zhanged19fc32017-10-19 13:06:22 -07001006 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001007 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001008
Colin Cross81440082018-08-15 20:21:55 -07001009 if len(javacFlags) > 0 {
1010 // optimization.
1011 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1012 flags.javacFlags = "$javacFlags"
1013 }
1014
Nan Zhanged19fc32017-10-19 13:06:22 -07001015 return flags
1016}
Colin Crossc0b06f12015-04-08 13:03:43 -07001017
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001018func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1019
Colin Crossebe1a512017-11-14 13:12:14 -08001020 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001021
1022 deps := j.collectDeps(ctx)
1023 flags := j.collectBuilderFlags(ctx, deps)
1024
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001025 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001026 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1027 }
Colin Cross8a497952019-03-05 22:25:09 -08001028 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001029 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001030 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001031 }
1032
Colin Crossaf050172017-11-15 23:01:59 -08001033 srcFiles = j.genSources(ctx, srcFiles, flags)
1034
1035 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001036 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001037 if aaptSrcJar != nil {
1038 srcJars = append(srcJars, aaptSrcJar)
1039 }
Colin Crossb7a63242015-04-16 14:09:14 -07001040
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001041 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1042 // that IDEInfo struct will use
1043 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1044
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001045 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001046 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001047 }
1048
Colin Cross1ee23172017-10-18 14:44:18 -07001049 jarName := ctx.ModuleName() + ".jar"
1050
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001051 javaSrcFiles := srcFiles.FilterByExt(".java")
1052 var uniqueSrcFiles android.Paths
1053 set := make(map[string]bool)
1054 for _, v := range javaSrcFiles {
1055 if _, found := set[v.String()]; !found {
1056 set[v.String()] = true
1057 uniqueSrcFiles = append(uniqueSrcFiles, v)
1058 }
1059 }
1060
Colin Cross55f63ea2018-08-27 12:37:09 -07001061 var kotlinJars android.Paths
1062
Colin Cross93e85952017-08-15 13:34:18 -07001063 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001064 // user defined kotlin flags.
1065 kotlincFlags := j.properties.Kotlincflags
1066 CheckKotlincFlags(ctx, kotlincFlags)
1067
Colin Cross93e85952017-08-15 13:34:18 -07001068 // If there are kotlin files, compile them first but pass all the kotlin and java files
1069 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1070 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001071 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001072 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001073 kotlincFlags = append(kotlincFlags, "-no-jdk")
1074 }
1075 if len(kotlincFlags) > 0 {
1076 // optimization.
1077 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1078 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001079 }
1080
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001081 var kotlinSrcFiles android.Paths
1082 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1083 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1084
Colin Crossafbb1732019-01-17 15:42:52 -08001085 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1086 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1087
1088 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1089 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1090
1091 if len(flags.processorPath) > 0 {
1092 // Use kapt for annotation processing
1093 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1094 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1095 srcJars = append(srcJars, kaptSrcJar)
1096 // Disable annotation processing in javac, it's already been handled by kapt
1097 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001098 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001099 }
Colin Cross93e85952017-08-15 13:34:18 -07001100
Colin Cross1ee23172017-10-18 14:44:18 -07001101 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001102 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001103 if ctx.Failed() {
1104 return
1105 }
1106
1107 // Make javac rule depend on the kotlinc rule
1108 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001109
Colin Cross93e85952017-08-15 13:34:18 -07001110 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001111 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001112 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001113 }
1114
Colin Cross55f63ea2018-08-27 12:37:09 -07001115 jars := append(android.Paths(nil), kotlinJars...)
1116
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001117 // Store the list of .java files that was passed to javac
1118 j.compiledJavaSrcs = uniqueSrcFiles
1119 j.compiledSrcJars = srcJars
1120
Nan Zhang61eaedb2017-11-02 13:28:15 -07001121 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001122 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001123 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1124 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001125 // Formerly, there was a check here that prevented annotation processors
1126 // from being used when sharding was enabled, as some annotation processors
1127 // do not function correctly in sharded environments. It was removed to
1128 // allow for the use of annotation processors that do function correctly
1129 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001130 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001131 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001132 if ctx.Failed() {
1133 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001134 }
1135 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001136 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001137 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001138 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001139 // If error-prone is enabled, add an additional rule to compile the java files into
1140 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001141 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001142 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1143 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001144 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001145 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001146 extraJarDeps = append(extraJarDeps, errorprone)
1147 }
1148
Nan Zhang61eaedb2017-11-02 13:28:15 -07001149 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001150 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001151 shardSize := int(*(j.properties.Javac_shard_size))
1152 var shardSrcs []android.Paths
1153 if len(uniqueSrcFiles) > 0 {
1154 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1155 for idx, shardSrc := range shardSrcs {
1156 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1157 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1158 jars = append(jars, classes)
1159 }
1160 }
1161 if len(srcJars) > 0 {
1162 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1163 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1164 jars = append(jars, classes)
1165 }
1166 } else {
1167 classes := android.PathForModuleOut(ctx, "javac", jarName)
1168 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1169 jars = append(jars, classes)
1170 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001171 if ctx.Config().EmitXrefRules() {
1172 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1173 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1174 j.kytheFiles = append(j.kytheFiles, extractionFile)
1175
1176 }
Colin Crossd6891432017-09-27 17:39:56 -07001177 if ctx.Failed() {
1178 return
1179 }
Colin Cross2fe66872015-03-30 17:20:39 -07001180 }
1181
Colin Cross0c4ce212019-05-03 15:28:19 -07001182 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1183
1184 var includeSrcJar android.WritablePath
1185 if Bool(j.properties.Include_srcs) {
1186 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1187 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1188 }
1189
Colin Crosscedd4762018-09-13 11:26:19 -07001190 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1191 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001192 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001193 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001194
1195 var resArgs []string
1196 var resDeps android.Paths
1197
1198 resArgs = append(resArgs, dirArgs...)
1199 resDeps = append(resDeps, dirDeps...)
1200
1201 resArgs = append(resArgs, fileArgs...)
1202 resDeps = append(resDeps, fileDeps...)
1203
Colin Cross988708c2019-05-06 14:04:11 -07001204 resArgs = append(resArgs, extraArgs...)
1205 resDeps = append(resDeps, extraDeps...)
1206
Colin Cross40a36712017-09-27 17:41:35 -07001207 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001208 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001209 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001210 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001211 if ctx.Failed() {
1212 return
1213 }
1214 }
1215
Colin Cross0c4ce212019-05-03 15:28:19 -07001216 var resourceJars android.Paths
1217 if j.resourceJar != nil {
1218 resourceJars = append(resourceJars, j.resourceJar)
1219 }
1220 if Bool(j.properties.Include_srcs) {
1221 resourceJars = append(resourceJars, includeSrcJar)
1222 }
1223 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001224
Colin Cross0c4ce212019-05-03 15:28:19 -07001225 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001226 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001227 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001228 false, nil, nil)
1229 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001230 } else if len(resourceJars) == 1 {
1231 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001232 }
1233
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001234 if len(deps.staticJars) > 0 {
1235 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001236 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001237
Colin Cross094054a2018-10-17 15:10:48 -07001238 manifest := j.overrideManifest
1239 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001240 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001241 }
Colin Cross635acc92017-09-12 22:50:46 -07001242
Colin Cross8a497952019-03-05 22:25:09 -08001243 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001244 if len(services) > 0 {
1245 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1246 var zipargs []string
1247 for _, file := range services {
1248 serviceFile := file.String()
1249 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1250 }
1251 ctx.Build(pctx, android.BuildParams{
1252 Rule: zip,
1253 Output: servicesJar,
1254 Implicits: services,
1255 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001256 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001257 },
1258 })
1259 jars = append(jars, servicesJar)
1260 }
1261
Colin Cross0a6e0072017-08-30 14:24:55 -07001262 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001263 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001264 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001265
1266 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001267 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1268 // Optimization: skip the combine step if there is nothing to do
1269 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1270 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1271 // any if len(jars) == 1.
1272 outputFile = moduleOutPath
1273 } else {
1274 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1275 ctx.Build(pctx, android.BuildParams{
1276 Rule: android.Cp,
1277 Input: jars[0],
1278 Output: combinedJar,
1279 })
1280 outputFile = combinedJar
1281 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001282 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001283 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001284 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001285 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001286 outputFile = combinedJar
1287 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001288
Colin Cross331a1212018-08-15 20:40:52 -07001289 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001290 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001291 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001292 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001293 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001294 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001295
1296 // jarjar resource jar if necessary
1297 if j.resourceJar != nil {
1298 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001299 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001300 j.resourceJar = resourceJarJarFile
1301 }
1302
Colin Cross0a6e0072017-08-30 14:24:55 -07001303 if ctx.Failed() {
1304 return
1305 }
1306 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001307
1308 // Check package restrictions if necessary.
1309 if len(j.properties.Permitted_packages) > 0 {
1310 // Check packages and copy to package-checked file.
1311 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1312 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1313 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1314
1315 if ctx.Failed() {
1316 return
1317 }
1318 }
1319
Nan Zhanged19fc32017-10-19 13:06:22 -07001320 j.implementationJarFile = outputFile
1321 if j.headerJarFile == nil {
1322 j.headerJarFile = j.implementationJarFile
1323 }
Colin Cross2fe66872015-03-30 17:20:39 -07001324
Colin Cross6510f912017-11-29 00:27:14 -08001325 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001326 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1327 j.properties.Instrument = true
1328 }
1329 }
1330
Colin Cross3144dfc2018-01-03 15:06:47 -08001331 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001332 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1333 }
1334
Colin Cross331a1212018-08-15 20:40:52 -07001335 // merge implementation jar with resources if necessary
1336 implementationAndResourcesJar := outputFile
1337 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001338 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001339 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001340 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001341 false, nil, nil)
1342 implementationAndResourcesJar = combinedJar
1343 }
1344
1345 j.implementationAndResourcesJar = implementationAndResourcesJar
1346
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001347 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001348 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001349 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001350 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001351 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001352 if ctx.Failed() {
1353 return
1354 }
Colin Cross331a1212018-08-15 20:40:52 -07001355
Jiyong Park09cb6292019-07-15 15:29:23 +09001356 // Hidden API CSV generation and dex encoding
1357 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1358 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001359
Colin Cross331a1212018-08-15 20:40:52 -07001360 // merge dex jar with resources if necessary
1361 if j.resourceJar != nil {
1362 jars := android.Paths{dexOutputFile, j.resourceJar}
1363 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1364 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1365 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001366 if j.deviceProperties.UncompressDex {
1367 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1368 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1369 dexOutputFile = combinedAlignedJar
1370 } else {
1371 dexOutputFile = combinedJar
1372 }
Colin Cross331a1212018-08-15 20:40:52 -07001373 }
1374
1375 j.dexJarFile = dexOutputFile
1376
Colin Cross8faf8fc2019-01-16 15:15:52 -08001377 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001378 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1379
1380 j.maybeStrippedDexJarFile = dexOutputFile
1381
Colin Cross3063b782018-08-15 11:19:12 -07001382 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001383
1384 if ctx.Failed() {
1385 return
1386 }
Colin Cross331a1212018-08-15 20:40:52 -07001387 } else {
1388 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001389 }
Colin Cross331a1212018-08-15 20:40:52 -07001390
Colin Crossb7a63242015-04-16 14:09:14 -07001391 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001392
1393 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1394 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001395}
1396
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001397// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1398// since some of these flags may be used internally.
1399func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1400 for _, flag := range flags {
1401 flag = strings.TrimSpace(flag)
1402
1403 if !strings.HasPrefix(flag, "-") {
1404 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1405 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1406 ctx.PropertyErrorf("kotlincflags",
1407 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1408 } else if inList(flag, config.KotlincIllegalFlags) {
1409 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1410 } else if flag == "-include-runtime" {
1411 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1412 } else {
1413 args := strings.Split(flag, " ")
1414 if args[0] == "-kotlin-home" {
1415 ctx.PropertyErrorf("kotlincflags",
1416 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1417 }
1418 }
1419 }
1420}
1421
Colin Cross8eadbf02017-10-24 17:46:00 -07001422func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001423 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001424
1425 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001426 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001427 // Compile java sources into turbine.jar.
1428 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1429 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1430 if ctx.Failed() {
1431 return nil
1432 }
1433 jars = append(jars, turbineJar)
1434 }
1435
Colin Cross55f63ea2018-08-27 12:37:09 -07001436 jars = append(jars, extraJars...)
1437
Nan Zhanged19fc32017-10-19 13:06:22 -07001438 // Combine any static header libraries into classes-header.jar. If there is only
1439 // one input jar this step will be skipped.
1440 var headerJar android.Path
1441 jars = append(jars, deps.staticHeaderJars...)
1442
Colin Cross5c6ecc12017-10-23 18:12:27 -07001443 // we cannot skip the combine step for now if there is only one jar
1444 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1445 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001446 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001447 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001448 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001449
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001450 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001451 // Transform classes.jar into classes-jarjar.jar
1452 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001453 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001454 headerJar = jarjarFile
1455 if ctx.Failed() {
1456 return nil
1457 }
1458 }
1459
1460 return headerJar
1461}
1462
Colin Crosscb933592017-11-22 13:49:43 -08001463func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001464 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001465
Colin Cross7a3139e2017-12-19 13:57:50 -08001466 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001467
Colin Cross84c38822018-01-03 15:59:46 -08001468 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001469 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1470
1471 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1472
1473 j.jacocoReportClassesFile = jacocoReportClassesFile
1474
1475 return instrumentedJar
1476}
1477
albaltai36ff7dc2018-12-25 14:35:23 +08001478var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001479
Nan Zhanged19fc32017-10-19 13:06:22 -07001480func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001481 if j.headerJarFile == nil {
1482 return nil
1483 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001484 return android.Paths{j.headerJarFile}
1485}
1486
1487func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001488 if j.implementationJarFile == nil {
1489 return nil
1490 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001491 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001492}
1493
Colin Crossf24a22a2019-01-31 14:12:44 -08001494func (j *Module) DexJar() android.Path {
1495 return j.dexJarFile
1496}
1497
Colin Cross331a1212018-08-15 20:40:52 -07001498func (j *Module) ResourceJars() android.Paths {
1499 if j.resourceJar == nil {
1500 return nil
1501 }
1502 return android.Paths{j.resourceJar}
1503}
1504
1505func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001506 if j.implementationAndResourcesJar == nil {
1507 return nil
1508 }
Colin Cross331a1212018-08-15 20:40:52 -07001509 return android.Paths{j.implementationAndResourcesJar}
1510}
1511
Colin Cross46c9b8b2017-06-22 16:51:17 -07001512func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001513 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001514 return j.exportAidlIncludeDirs
1515}
1516
Jiyong Park1be96912018-05-28 18:02:19 +09001517func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001518 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001519 return j.exportedSdkLibs
1520}
1521
Colin Cross0c4ce212019-05-03 15:28:19 -07001522func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1523 return j.srcJarArgs, j.srcJarDeps
1524}
1525
Colin Cross46c9b8b2017-06-22 16:51:17 -07001526var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001527
Colin Cross46c9b8b2017-06-22 16:51:17 -07001528func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001529 return j.logtagsSrcs
1530}
1531
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001532// Collect information for opening IDE project files in java/jdeps.go.
1533func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1534 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1535 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001536 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001537 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001538 if j.expandJarjarRules != nil {
1539 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001540 }
1541}
1542
1543func (j *Module) CompilerDeps() []string {
1544 jdeps := []string{}
1545 jdeps = append(jdeps, j.properties.Libs...)
1546 jdeps = append(jdeps, j.properties.Static_libs...)
1547 return jdeps
1548}
1549
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001550func (j *Module) hasCode(ctx android.ModuleContext) bool {
1551 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1552 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1553}
1554
Colin Cross2fe66872015-03-30 17:20:39 -07001555//
1556// Java libraries (.jar file)
1557//
1558
Colin Crossf506d872017-07-19 15:53:04 -07001559type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001560 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001561}
1562
Colin Cross42be7612019-02-21 18:12:14 -08001563func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001564 // Store uncompressed (and do not strip) dex files from boot class path jars.
1565 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1566 return true
1567 }
1568
1569 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001570 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001571 return true
1572 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001573 if ctx.Config().UncompressPrivAppDex() &&
1574 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1575 return true
1576 }
1577
Colin Cross2fc72f62018-12-21 12:59:54 -08001578 return false
1579}
1580
Colin Crossf506d872017-07-19 15:53:04 -07001581func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001582 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1583 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001584 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001585 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001586 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001587 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001588
Jiyong Park7f7766d2019-07-25 22:02:35 +09001589 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1590 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001591 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1592 ctx.ModuleName()+".jar", j.outputFile)
1593 }
Colin Crossb7a63242015-04-16 14:09:14 -07001594}
1595
Colin Crossf506d872017-07-19 15:53:04 -07001596func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001597 j.deps(ctx)
1598}
1599
Colin Cross1b16b0e2019-02-12 14:41:32 -08001600// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1601//
1602// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1603// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1604// as a `static_libs` dependency of another module.
1605//
1606// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1607// a device.
1608//
1609// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1610// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001611func LibraryFactory() android.Module {
1612 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001613
Colin Cross9ae1b922018-06-26 17:59:05 -07001614 module.AddProperties(
1615 &module.Module.properties,
1616 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001617 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001618 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001619
Colin Cross9ae1b922018-06-26 17:59:05 -07001620 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001621 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001622 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001623}
1624
Colin Cross1b16b0e2019-02-12 14:41:32 -08001625// java_library_static is an obsolete alias for java_library.
1626func LibraryStaticFactory() android.Module {
1627 return LibraryFactory()
1628}
1629
1630// java_library_host builds and links sources into a `.jar` file for the host.
1631//
1632// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1633// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001634func LibraryHostFactory() android.Module {
1635 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001636
Colin Cross6af17aa2017-09-20 12:59:05 -07001637 module.AddProperties(
1638 &module.Module.properties,
1639 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001640
Colin Cross9ae1b922018-06-26 17:59:05 -07001641 module.Module.properties.Installable = proptools.BoolPtr(true)
1642
Colin Cross89536d42017-07-07 14:35:50 -07001643 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001644 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001645 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001646}
1647
1648//
Colin Crossb628ea52018-08-14 16:42:33 -07001649// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001650//
1651
1652type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001653 // list of compatibility suites (for example "cts", "vts") that the module should be
1654 // installed into.
1655 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001656
1657 // the name of the test configuration (for example "AndroidTest.xml") that should be
1658 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001659 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001660
Jack He33338892018-09-19 02:21:28 -07001661 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1662 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001663 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001664
Colin Crossd96ca352018-08-10 16:06:24 -07001665 // list of files or filegroup modules that provide data that should be installed alongside
1666 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001667 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001668}
1669
Paul Duffin42df1442019-03-20 12:45:53 +00001670type testHelperLibraryProperties struct {
1671 // list of compatibility suites (for example "cts", "vts") that the module should be
1672 // installed into.
1673 Test_suites []string `android:"arch_variant"`
1674}
1675
Colin Cross05638fc2018-04-09 18:40:24 -07001676type Test struct {
1677 Library
1678
1679 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001680
1681 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001682 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001683}
1684
Paul Duffin42df1442019-03-20 12:45:53 +00001685type TestHelperLibrary struct {
1686 Library
1687
1688 testHelperLibraryProperties testHelperLibraryProperties
1689}
1690
Colin Cross303e21f2018-08-07 16:49:25 -07001691func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001692 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 -08001693 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001694
1695 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001696}
1697
Paul Duffin42df1442019-03-20 12:45:53 +00001698func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1699 j.Library.GenerateAndroidBuildActions(ctx)
1700}
1701
Colin Cross1b16b0e2019-02-12 14:41:32 -08001702// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1703// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1704//
1705// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1706// compiled against the device bootclasspath.
1707//
1708// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1709// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001710func TestFactory() android.Module {
1711 module := &Test{}
1712
1713 module.AddProperties(
1714 &module.Module.properties,
1715 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001716 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001717 &module.Module.protoProperties,
1718 &module.testProperties)
1719
Colin Cross9ae1b922018-06-26 17:59:05 -07001720 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001721 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001722
Colin Cross05638fc2018-04-09 18:40:24 -07001723 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001724 return module
1725}
1726
Paul Duffin42df1442019-03-20 12:45:53 +00001727// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1728func TestHelperLibraryFactory() android.Module {
1729 module := &TestHelperLibrary{}
1730
1731 module.AddProperties(
1732 &module.Module.properties,
1733 &module.Module.deviceProperties,
1734 &module.Module.dexpreoptProperties,
1735 &module.Module.protoProperties,
1736 &module.testHelperLibraryProperties)
1737
Colin Cross9a4abed2019-04-24 13:19:28 -07001738 module.Module.properties.Installable = proptools.BoolPtr(true)
1739 module.Module.dexpreopter.isTest = true
1740
Paul Duffin42df1442019-03-20 12:45:53 +00001741 InitJavaModule(module, android.HostAndDeviceSupported)
1742 return module
1743}
1744
Colin Cross1b16b0e2019-02-12 14:41:32 -08001745// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1746// allow running the test with `atest` or a `TEST_MAPPING` file.
1747//
1748// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1749// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001750func TestHostFactory() android.Module {
1751 module := &Test{}
1752
1753 module.AddProperties(
1754 &module.Module.properties,
1755 &module.Module.protoProperties,
1756 &module.testProperties)
1757
Colin Cross9ae1b922018-06-26 17:59:05 -07001758 module.Module.properties.Installable = proptools.BoolPtr(true)
1759
Colin Cross05638fc2018-04-09 18:40:24 -07001760 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001761 return module
1762}
1763
1764//
Colin Cross2fe66872015-03-30 17:20:39 -07001765// Java Binaries (.jar file plus wrapper script)
1766//
1767
Colin Crossf506d872017-07-19 15:53:04 -07001768type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001769 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001770 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001771
1772 // Name of the class containing main to be inserted into the manifest as Main-Class.
1773 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001774}
1775
Colin Crossf506d872017-07-19 15:53:04 -07001776type Binary struct {
1777 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001778
Colin Crossf506d872017-07-19 15:53:04 -07001779 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001780
Colin Cross6b4a32d2017-12-05 13:42:45 -08001781 isWrapperVariant bool
1782
Colin Crossc3315992017-12-08 19:12:36 -08001783 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001784 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001785}
1786
Alex Light24237172017-10-26 09:46:21 -07001787func (j *Binary) HostToolPath() android.OptionalPath {
1788 return android.OptionalPathForPath(j.binaryFile)
1789}
1790
Colin Crossf506d872017-07-19 15:53:04 -07001791func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001792 if ctx.Arch().ArchType == android.Common {
1793 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001794 if j.binaryProperties.Main_class != nil {
1795 if j.properties.Manifest != nil {
1796 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1797 }
1798 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1799 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1800 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1801 }
1802
Colin Cross6b4a32d2017-12-05 13:42:45 -08001803 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001804 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001805 // Handle the binary wrapper
1806 j.isWrapperVariant = true
1807
Colin Cross366938f2017-12-11 16:29:02 -08001808 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001809 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001810 } else {
1811 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1812 }
1813
1814 // Depend on the installed jar so that the wrapper doesn't get executed by
1815 // another build rule before the jar has been installed.
1816 jarFile := ctx.PrimaryModule().(*Binary).installFile
1817
1818 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1819 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001820 }
Colin Cross2fe66872015-03-30 17:20:39 -07001821}
1822
Colin Crossf506d872017-07-19 15:53:04 -07001823func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001824 if ctx.Arch().ArchType == android.Common {
1825 j.deps(ctx)
1826 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001827}
1828
Colin Cross1b16b0e2019-02-12 14:41:32 -08001829// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1830// as well.
1831//
1832// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1833// compiled against the device bootclasspath.
1834//
1835// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1836// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001837func BinaryFactory() android.Module {
1838 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001839
Colin Cross36242852017-06-23 15:06:31 -07001840 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001841 &module.Module.properties,
1842 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001843 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001844 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001845 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001846
Colin Cross9ae1b922018-06-26 17:59:05 -07001847 module.Module.properties.Installable = proptools.BoolPtr(true)
1848
Colin Cross6b4a32d2017-12-05 13:42:45 -08001849 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1850 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001851 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001852}
1853
Colin Cross1b16b0e2019-02-12 14:41:32 -08001854// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1855//
1856// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1857// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001858func BinaryHostFactory() android.Module {
1859 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001860
Colin Cross36242852017-06-23 15:06:31 -07001861 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001862 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001863 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001864 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001865
Colin Cross9ae1b922018-06-26 17:59:05 -07001866 module.Module.properties.Installable = proptools.BoolPtr(true)
1867
Colin Cross6b4a32d2017-12-05 13:42:45 -08001868 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1869 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001870 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001871}
1872
1873//
1874// Java prebuilts
1875//
1876
Colin Cross74d73e22017-08-02 11:05:49 -07001877type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001878 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001879
Nan Zhangea568a42017-11-08 21:20:04 -08001880 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001881
1882 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001883
1884 // List of shared java libs that this module has dependencies to
1885 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001886
1887 // List of files to remove from the jar file(s)
1888 Exclude_files []string
1889
1890 // List of directories to remove from the jar file(s)
1891 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001892
1893 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001894 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001895}
1896
1897type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001898 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001899 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001900 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001901 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001902
Colin Cross74d73e22017-08-02 11:05:49 -07001903 properties ImportProperties
1904
Colin Cross0a6e0072017-08-30 14:24:55 -07001905 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001906 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001907}
1908
Colin Cross83bb3162018-06-25 15:48:06 -07001909func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001910 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001911}
1912
1913func (j *Import) minSdkVersion() string {
1914 return j.sdkVersion()
1915}
1916
Colin Cross74d73e22017-08-02 11:05:49 -07001917func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001918 return &j.prebuilt
1919}
1920
Colin Cross74d73e22017-08-02 11:05:49 -07001921func (j *Import) PrebuiltSrcs() []string {
1922 return j.properties.Jars
1923}
1924
1925func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001926 return j.prebuilt.Name(j.ModuleBase.Name())
1927}
1928
Colin Cross74d73e22017-08-02 11:05:49 -07001929func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001930 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001931}
1932
Colin Cross74d73e22017-08-02 11:05:49 -07001933func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001934 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001935
Nan Zhang4c819fb2018-08-27 18:31:46 -07001936 jarName := ctx.ModuleName() + ".jar"
1937 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001938 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1939 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001940 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001941 inputFile := outputFile
1942 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1943 TransformJetifier(ctx, outputFile, inputFile)
1944 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001945 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001946
1947 ctx.VisitDirectDeps(func(module android.Module) {
1948 otherName := ctx.OtherModuleName(module)
1949 tag := ctx.OtherModuleDependencyTag(module)
1950
1951 switch dep := module.(type) {
1952 case Dependency:
1953 switch tag {
1954 case libTag, staticLibTag:
1955 // sdk lib names from dependencies are re-exported
1956 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1957 }
1958 case SdkLibraryDependency:
1959 switch tag {
1960 case libTag:
1961 // names of sdk libs that are directly depended are exported
1962 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1963 }
1964 }
1965 })
1966
1967 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001968 if Bool(j.properties.Installable) {
1969 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1970 ctx.ModuleName()+".jar", outputFile)
1971 }
Colin Cross2fe66872015-03-30 17:20:39 -07001972}
1973
Colin Cross74d73e22017-08-02 11:05:49 -07001974var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001975
Nan Zhanged19fc32017-10-19 13:06:22 -07001976func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001977 if j.combinedClasspathFile == nil {
1978 return nil
1979 }
Colin Cross37f6d792018-07-12 12:28:41 -07001980 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001981}
1982
1983func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001984 if j.combinedClasspathFile == nil {
1985 return nil
1986 }
Colin Cross37f6d792018-07-12 12:28:41 -07001987 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001988}
1989
Colin Cross331a1212018-08-15 20:40:52 -07001990func (j *Import) ResourceJars() android.Paths {
1991 return nil
1992}
1993
1994func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001995 if j.combinedClasspathFile == nil {
1996 return nil
1997 }
Colin Cross331a1212018-08-15 20:40:52 -07001998 return android.Paths{j.combinedClasspathFile}
1999}
2000
Colin Crossf24a22a2019-01-31 14:12:44 -08002001func (j *Import) DexJar() android.Path {
2002 return nil
2003}
2004
Colin Cross74d73e22017-08-02 11:05:49 -07002005func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002006 return nil
2007}
2008
Jiyong Park1be96912018-05-28 18:02:19 +09002009func (j *Import) ExportedSdkLibs() []string {
2010 return j.exportedSdkLibs
2011}
2012
Colin Cross0c4ce212019-05-03 15:28:19 -07002013func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2014 return nil, nil
2015}
2016
albaltai36ff7dc2018-12-25 14:35:23 +08002017// Add compile time check for interface implementation
2018var _ android.IDEInfo = (*Import)(nil)
2019var _ android.IDECustomizedModuleName = (*Import)(nil)
2020
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002021// Collect information for opening IDE project files in java/jdeps.go.
2022const (
2023 removedPrefix = "prebuilt_"
2024)
2025
2026func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2027 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2028}
2029
2030func (j *Import) IDECustomizedModuleName() string {
2031 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2032 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2033 // solution to get the Import name.
2034 name := j.Name()
2035 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002036 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002037 }
2038 return name
2039}
2040
Colin Cross74d73e22017-08-02 11:05:49 -07002041var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002042
Colin Cross1b16b0e2019-02-12 14:41:32 -08002043// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2044//
2045// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2046// compiled against an Android classpath.
2047//
2048// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2049// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002050func ImportFactory() android.Module {
2051 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002052
Colin Cross74d73e22017-08-02 11:05:49 -07002053 module.AddProperties(&module.properties)
2054
2055 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002056 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002057 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002058 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002059}
2060
Colin Cross1b16b0e2019-02-12 14:41:32 -08002061// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2062// module.
2063//
2064// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2065// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002066func ImportFactoryHost() android.Module {
2067 module := &Import{}
2068
2069 module.AddProperties(&module.properties)
2070
2071 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002072 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002073 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002074 return module
2075}
2076
Colin Cross42be7612019-02-21 18:12:14 -08002077// dex_import module
2078
2079type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002080 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002081}
2082
2083type DexImport struct {
2084 android.ModuleBase
2085 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002086 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002087 prebuilt android.Prebuilt
2088
2089 properties DexImportProperties
2090
2091 dexJarFile android.Path
2092 maybeStrippedDexJarFile android.Path
2093
2094 dexpreopter
2095}
2096
2097func (j *DexImport) Prebuilt() *android.Prebuilt {
2098 return &j.prebuilt
2099}
2100
2101func (j *DexImport) PrebuiltSrcs() []string {
2102 return j.properties.Jars
2103}
2104
2105func (j *DexImport) Name() string {
2106 return j.prebuilt.Name(j.ModuleBase.Name())
2107}
2108
Colin Cross42be7612019-02-21 18:12:14 -08002109func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2110 if len(j.properties.Jars) != 1 {
2111 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2112 }
2113
2114 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2115 j.dexpreopter.isInstallable = true
2116 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2117
2118 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2119 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2120
2121 if j.dexpreopter.uncompressedDex {
2122 rule := android.NewRuleBuilder()
2123
2124 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2125 rule.Temporary(temporary)
2126
2127 // use zip2zip to uncompress classes*.dex files
2128 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002129 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002130 FlagWithInput("-i ", inputJar).
2131 FlagWithOutput("-o ", temporary).
2132 FlagWithArg("-0 ", "'classes*.dex'")
2133
2134 // use zipalign to align uncompressed classes*.dex files
2135 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002136 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002137 Flag("-f").
2138 Text("4").
2139 Input(temporary).
2140 Output(dexOutputFile)
2141
2142 rule.DeleteTemporaryFiles()
2143
2144 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2145 } else {
2146 ctx.Build(pctx, android.BuildParams{
2147 Rule: android.Cp,
2148 Input: inputJar,
2149 Output: dexOutputFile,
2150 })
2151 }
2152
2153 j.dexJarFile = dexOutputFile
2154
2155 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2156
2157 j.maybeStrippedDexJarFile = dexOutputFile
2158
2159 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2160 ctx.ModuleName()+".jar", dexOutputFile)
2161}
2162
2163func (j *DexImport) DexJar() android.Path {
2164 return j.dexJarFile
2165}
2166
2167// dex_import imports a `.jar` file containing classes.dex files.
2168//
2169// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2170// to the device.
2171func DexImportFactory() android.Module {
2172 module := &DexImport{}
2173
2174 module.AddProperties(&module.properties)
2175
2176 android.InitPrebuiltModule(module, &module.properties.Jars)
2177 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002178 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002179 return module
2180}
2181
Colin Cross89536d42017-07-07 14:35:50 -07002182//
2183// Defaults
2184//
2185type Defaults struct {
2186 android.ModuleBase
2187 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002188 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002189}
2190
Colin Cross1b16b0e2019-02-12 14:41:32 -08002191// java_defaults provides a set of properties that can be inherited by other java or android modules.
2192//
2193// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2194// property in the defaults module that exists in the depending module will be prepended to the depending module's
2195// value for that property.
2196//
2197// Example:
2198//
2199// java_defaults {
2200// name: "example_defaults",
2201// srcs: ["common/**/*.java"],
2202// javacflags: ["-Xlint:all"],
2203// aaptflags: ["--auto-add-overlay"],
2204// }
2205//
2206// java_library {
2207// name: "example",
2208// defaults: ["example_defaults"],
2209// srcs: ["example/**/*.java"],
2210// }
2211//
2212// is functionally identical to:
2213//
2214// java_library {
2215// name: "example",
2216// srcs: [
2217// "common/**/*.java",
2218// "example/**/*.java",
2219// ],
2220// javacflags: ["-Xlint:all"],
2221// }
Colin Cross89536d42017-07-07 14:35:50 -07002222func defaultsFactory() android.Module {
2223 return DefaultsFactory()
2224}
2225
2226func DefaultsFactory(props ...interface{}) android.Module {
2227 module := &Defaults{}
2228
2229 module.AddProperties(props...)
2230 module.AddProperties(
2231 &CompilerProperties{},
2232 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002233 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002234 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002235 &aaptProperties{},
2236 &androidLibraryProperties{},
2237 &appProperties{},
2238 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002239 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002240 &ImportProperties{},
2241 &AARImportProperties{},
2242 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002243 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002244 )
2245
2246 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002247 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002248 return module
2249}
Nan Zhangea568a42017-11-08 21:20:04 -08002250
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002251func kytheExtractJavaFactory() android.Singleton {
2252 return &kytheExtractJavaSingleton{}
2253}
2254
2255type kytheExtractJavaSingleton struct {
2256}
2257
2258func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2259 var xrefTargets android.Paths
2260 ctx.VisitAllModules(func(module android.Module) {
2261 if javaModule, ok := module.(xref); ok {
2262 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2263 }
2264 })
2265 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2266 if len(xrefTargets) > 0 {
2267 ctx.Build(pctx, android.BuildParams{
2268 Rule: blueprint.Phony,
2269 Output: android.PathForPhony(ctx, "xref_java"),
2270 Inputs: xrefTargets,
2271 })
2272 }
2273}
2274
Nan Zhangea568a42017-11-08 21:20:04 -08002275var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002276var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002277var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002278var inList = android.InList