blob: fea38b51fe811eb8eb74c74781f3fa61d287c6ba [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 Cross41955e82019-05-29 14:40:35 -0700373 default:
374 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
375 }
Colin Cross54250902017-12-05 09:28:08 -0800376}
377
Jiyong Park8fd61922018-11-08 02:50:25 +0900378func (j *Module) DexJarFile() android.Path {
379 return j.dexJarFile
380}
381
Colin Cross41955e82019-05-29 14:40:35 -0700382var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800383
Colin Crossf506d872017-07-19 15:53:04 -0700384type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700385 HeaderJars() android.Paths
386 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700387 ResourceJars() android.Paths
388 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800389 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700390 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900391 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700392 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700393}
394
Jiyong Parkc678ad32018-04-10 13:07:10 +0900395type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700396 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
397 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398}
399
Nan Zhangb2b33de2018-02-23 11:18:47 -0800400type SrcDependency interface {
401 CompiledSrcs() android.Paths
402 CompiledSrcJars() android.Paths
403}
404
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800405type xref interface {
406 XrefJavaFiles() android.Paths
407}
408
Nan Zhangb2b33de2018-02-23 11:18:47 -0800409func (j *Module) CompiledSrcs() android.Paths {
410 return j.compiledJavaSrcs
411}
412
413func (j *Module) CompiledSrcJars() android.Paths {
414 return j.compiledSrcJars
415}
416
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800417func (j *Module) XrefJavaFiles() android.Paths {
418 return j.kytheFiles
419}
420
Nan Zhangb2b33de2018-02-23 11:18:47 -0800421var _ SrcDependency = (*Module)(nil)
422
Colin Cross89536d42017-07-07 14:35:50 -0700423func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
424 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
425 android.InitDefaultableModule(module)
426}
427
Colin Crossbe1da472017-07-07 15:59:46 -0700428type dependencyTag struct {
429 blueprint.BaseDependencyTag
430 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700431}
432
Colin Crossa4f08812018-10-02 22:03:40 -0700433type jniDependencyTag struct {
434 blueprint.BaseDependencyTag
435 target android.Target
436}
437
Colin Crossbe1da472017-07-07 15:59:46 -0700438var (
Colin Cross4b964c02018-10-15 16:18:06 -0700439 staticLibTag = dependencyTag{name: "staticlib"}
440 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800441 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700442 bootClasspathTag = dependencyTag{name: "bootclasspath"}
443 systemModulesTag = dependencyTag{name: "system modules"}
444 frameworkResTag = dependencyTag{name: "framework-res"}
445 frameworkApkTag = dependencyTag{name: "framework-apk"}
446 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800447 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700448 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
449 certificateTag = dependencyTag{name: "certificate"}
450 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700451 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700452)
Colin Cross2fe66872015-03-30 17:20:39 -0700453
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900454func defaultSdkVersion(ctx checkVendorModuleContext) string {
455 if ctx.SocSpecific() || ctx.DeviceSpecific() {
456 return "system_current"
457 }
458 return ""
459}
460
461type checkVendorModuleContext interface {
462 SocSpecific() bool
463 DeviceSpecific() bool
464}
465
Colin Crossfc3674a2017-09-18 17:41:52 -0700466type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700467 useModule, useFiles, useDefaultLibs, invalidVersion bool
468
Colin Cross86a60ae2018-05-29 14:44:55 -0700469 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700470 systemModules string
471
Colin Crossa97c5d32018-03-28 14:58:31 -0700472 frameworkResModule string
473
Colin Cross86a60ae2018-05-29 14:44:55 -0700474 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700475 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100476
477 noStandardLibs, noFrameworksLibs bool
478}
479
480func (s sdkDep) hasStandardLibs() bool {
481 return !s.noStandardLibs
482}
483
484func (s sdkDep) hasFrameworkLibs() bool {
485 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700486}
487
Colin Crossa4f08812018-10-02 22:03:40 -0700488type jniLib struct {
489 name string
490 path android.Path
491 target android.Target
492}
493
Colin Cross0ea8ba82019-06-06 14:33:29 -0700494func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800495 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
496}
497
Colin Cross0ea8ba82019-06-06 14:33:29 -0700498func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800499 return j.shouldInstrument(ctx) &&
500 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
501 ctx.Config().UnbundledBuild())
502}
503
Colin Cross83bb3162018-06-25 15:48:06 -0700504func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900505 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700506}
507
508func (j *Module) minSdkVersion() string {
509 if j.deviceProperties.Min_sdk_version != nil {
510 return *j.deviceProperties.Min_sdk_version
511 }
512 return j.sdkVersion()
513}
514
Dan Willemsen419290a2018-10-31 15:28:47 -0700515func (j *Module) targetSdkVersion() string {
516 if j.deviceProperties.Target_sdk_version != nil {
517 return *j.deviceProperties.Target_sdk_version
518 }
519 return j.sdkVersion()
520}
521
Colin Crossbe1da472017-07-07 15:59:46 -0700522func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700523 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100524 sdkDep := decodeSdkDep(ctx, sdkContext(j))
525 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700526 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700527 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100528 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100529 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700530 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700531 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700532 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100533 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700534 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700535 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700536 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
537 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800538 }
Colin Crossbe1da472017-07-07 15:59:46 -0700539 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700540 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100541 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100542 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100543 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700544 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700545 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100546 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700547 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800548 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800549 if ctx.ModuleName() == "android_stubs_current" ||
550 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700551 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700552 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800553 }
Colin Cross2fe66872015-03-30 17:20:39 -0700554 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700555
Colin Cross42d48b72018-08-29 14:10:52 -0700556 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
557 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700558
Colin Crossbe9cdb82019-01-21 21:37:16 -0800559 ctx.AddFarVariationDependencies([]blueprint.Variation{
560 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
561 }, pluginTag, j.properties.Plugins...)
562
Colin Crossfe17f6f2019-03-28 19:30:56 -0700563 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700564 if j.hasSrcExt(".proto") {
565 protoDeps(ctx, &j.protoProperties)
566 }
Colin Cross93e85952017-08-15 13:34:18 -0700567
568 if j.hasSrcExt(".kt") {
569 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
570 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700571 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
572 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800573 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800574 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
575 }
Colin Cross93e85952017-08-15 13:34:18 -0700576 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800577
578 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700579 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800580 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700581}
582
583func hasSrcExt(srcs []string, ext string) bool {
584 for _, src := range srcs {
585 if filepath.Ext(src) == ext {
586 return true
587 }
588 }
589
590 return false
591}
592
Nan Zhang61eaedb2017-11-02 13:28:15 -0700593func shardPaths(paths android.Paths, shardSize int) []android.Paths {
594 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
595 for len(paths) > shardSize {
596 ret = append(ret, paths[0:shardSize])
597 paths = paths[shardSize:]
598 }
599 if len(paths) > 0 {
600 ret = append(ret, paths)
601 }
602 return ret
603}
604
Colin Cross6af17aa2017-09-20 12:59:05 -0700605func (j *Module) hasSrcExt(ext string) bool {
606 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700607}
608
Colin Cross46c9b8b2017-06-22 16:51:17 -0700609func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700610 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700611
Colin Crossebe1a512017-11-14 13:12:14 -0800612 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
613 aidlIncludes = append(aidlIncludes,
614 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
615 aidlIncludes = append(aidlIncludes,
616 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700617
Colin Cross3047fa22019-04-18 10:56:44 -0700618 var flags []string
619 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700620
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700621 if aidlPreprocess.Valid() {
622 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700623 deps = append(deps, aidlPreprocess.Path())
624 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700625 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700626 }
627
Colin Cross3047fa22019-04-18 10:56:44 -0700628 if len(j.exportAidlIncludeDirs) > 0 {
629 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
630 }
631
632 if len(aidlIncludes) > 0 {
633 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
634 }
635
Colin Cross635c3b02016-05-18 15:37:25 -0700636 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800637 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700638 flags = append(flags, "-I"+src.String())
639 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700640
Martijn Coeneneab15642018-03-09 09:29:59 +0100641 if Bool(j.deviceProperties.Aidl.Generate_traces) {
642 flags = append(flags, "-t")
643 }
644
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100645 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
646 flags = append(flags, "--transaction_names")
647 }
648
Colin Cross3047fa22019-04-18 10:56:44 -0700649 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700650}
651
Colin Cross32f676a2017-09-06 13:41:06 -0700652type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800653 classpath classpath
654 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700655 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800656 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700657 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700658 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700659 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700660 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800661 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700662 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700663 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700664 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700665 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800666 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800667
668 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700669}
Colin Cross2fe66872015-03-30 17:20:39 -0700670
Colin Cross54250902017-12-05 09:28:08 -0800671func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
672 for _, f := range dep.Srcs() {
673 if f.Ext() != ".jar" {
674 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
675 ctx.OtherModuleName(dep.(blueprint.Module)))
676 }
677 }
678}
679
Jiyong Park2d492942018-03-05 17:44:10 +0900680type linkType int
681
682const (
683 javaCore linkType = iota
684 javaSdk
685 javaSystem
686 javaPlatform
687)
688
Jiyong Park46f78fb2018-10-20 16:33:17 +0900689func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700690 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700691 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900692 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
693 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100694 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900695 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100696 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900697 return javaCore, false
698 case name == "android_system_stubs_current":
699 return javaSystem, true
700 case strings.HasPrefix(ver, "system_"):
701 return javaSystem, false
702 case name == "android_test_stubs_current":
703 return javaSystem, true
704 case strings.HasPrefix(ver, "test_"):
705 return javaPlatform, false
706 case name == "android_stubs_current":
707 return javaSdk, true
708 case ver == "current":
709 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100710 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900711 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700712 default:
713 if _, err := strconv.Atoi(ver); err != nil {
714 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
715 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900716 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900717 }
718}
719
Jiyong Park750e5572018-01-31 00:20:13 +0900720func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700721 if ctx.Host() {
722 return
723 }
724
Jiyong Park46f78fb2018-10-20 16:33:17 +0900725 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
726 if stubs {
727 return
728 }
729 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900730 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."
731
732 switch myLinkType {
733 case javaCore:
734 if otherLinkType != javaCore {
735 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 +0900736 ctx.OtherModuleName(to))
737 }
Jiyong Park2d492942018-03-05 17:44:10 +0900738 break
739 case javaSdk:
740 if otherLinkType != javaCore && otherLinkType != javaSdk {
741 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
742 ctx.OtherModuleName(to))
743 }
744 break
745 case javaSystem:
746 if otherLinkType == javaPlatform {
747 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
748 ctx.OtherModuleName(to))
749 }
750 break
751 case javaPlatform:
752 // no restriction on link-type
753 break
Jiyong Park750e5572018-01-31 00:20:13 +0900754 }
755}
756
Colin Cross32f676a2017-09-06 13:41:06 -0700757func (j *Module) collectDeps(ctx android.ModuleContext) deps {
758 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700759
Colin Cross300f0382018-03-06 13:11:51 -0800760 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700761 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800762 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700763 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800764 } else if sdkDep.useFiles {
765 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700766 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700767 deps.aidlPreprocess = sdkDep.aidl
768 } else {
769 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800770 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700771 }
772
Colin Crossd11fcda2017-10-23 17:59:01 -0700773 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700774 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700775 tag := ctx.OtherModuleDependencyTag(module)
776
Colin Crossa4f08812018-10-02 22:03:40 -0700777 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700778 // Handled by AndroidApp.collectAppDeps
779 return
780 }
781 if tag == certificateTag {
782 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700783 return
784 }
785
Jiyong Park750e5572018-01-31 00:20:13 +0900786 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700787 switch tag {
788 case bootClasspathTag, libTag, staticLibTag:
789 checkLinkType(ctx, j, to, tag.(dependencyTag))
790 }
Jiyong Park750e5572018-01-31 00:20:13 +0900791 }
Colin Cross54250902017-12-05 09:28:08 -0800792 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800793 case SdkLibraryDependency:
794 switch tag {
795 case libTag:
796 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
797 // names of sdk libs that are directly depended are exported
798 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700799 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800800 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
801 }
Colin Cross54250902017-12-05 09:28:08 -0800802 case Dependency:
803 switch tag {
804 case bootClasspathTag:
805 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700806 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800807 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900808 // sdk lib names from dependencies are re-exported
809 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700810 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800811 case staticLibTag:
812 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
813 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
814 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700815 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900816 // sdk lib names from dependencies are re-exported
817 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700818 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800819 case pluginTag:
820 if plugin, ok := dep.(*Plugin); ok {
821 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
822 if plugin.pluginProperties.Processor_class != nil {
823 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
824 }
825 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
826 } else {
827 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
828 }
Colin Cross54250902017-12-05 09:28:08 -0800829 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100830 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800831 // framework.jar has a one-off dependency on the R.java and Manifest.java files
832 // generated by framework-res.apk
833 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
834 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800835 case frameworkApkTag:
836 if ctx.ModuleName() == "android_stubs_current" ||
837 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700838 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800839 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
840 // resource files out of there for aapt.
841 //
842 // Normally the package rule runs aapt, which includes the resource,
843 // but we're not running that in our package rule so just copy in the
844 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700845 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800846 }
Colin Cross54250902017-12-05 09:28:08 -0800847 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700848 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800849 case kotlinAnnotationsTag:
850 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800851 }
852
Colin Cross54250902017-12-05 09:28:08 -0800853 case android.SourceFileProducer:
854 switch tag {
855 case libTag:
856 checkProducesJars(ctx, dep)
857 deps.classpath = append(deps.classpath, dep.Srcs()...)
858 case staticLibTag:
859 checkProducesJars(ctx, dep)
860 deps.classpath = append(deps.classpath, dep.Srcs()...)
861 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
862 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800863 }
864 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700865 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700866 case systemModulesTag:
867 if deps.systemModules != nil {
868 panic("Found two system module dependencies")
869 }
870 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000871 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700872 panic("Missing directory for system module dependency")
873 }
Colin Crossb77043e2019-07-16 13:57:13 -0700874 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700875 }
Colin Crossec7a0422017-07-07 14:47:12 -0700876 }
Colin Cross2fe66872015-03-30 17:20:39 -0700877 })
878
Jiyong Park1be96912018-05-28 18:02:19 +0900879 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
880
Colin Cross32f676a2017-09-06 13:41:06 -0700881 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700882}
883
Colin Cross83bb3162018-06-25 15:48:06 -0700884func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700885 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800886 v := sdkContext.sdkVersion()
887 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100888 if ctx.Config().IsPdkBuild() &&
889 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700890 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800891 latestSdkVersion := 0
892 if len(sdkVersions) > 0 {
893 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
894 }
895 v = strconv.Itoa(latestSdkVersion)
896 }
897
898 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700899 if err != nil {
900 ctx.PropertyErrorf("sdk_version", "%s", err)
901 }
Nan Zhang357466b2018-04-17 17:38:36 -0700902 if javaVersion != "" {
903 ret = javaVersion
904 } else if ctx.Device() && sdk <= 23 {
905 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100906 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700907 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100908 } else if ctx.Device() &&
909 sdkContext.sdkVersion() != "" &&
910 sdkContext.sdkVersion() != "none" &&
911 sdkContext.sdkVersion() != "core_platform" &&
912 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700913 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
914 ret = "1.8"
915 } else {
916 ret = "1.9"
917 }
918
919 return ret
920}
921
Nan Zhanged19fc32017-10-19 13:06:22 -0700922func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700923
Colin Crossf03c82b2015-04-13 13:53:40 -0700924 var flags javaBuilderFlags
925
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100926 // javaVersion flag.
927 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
928
Nan Zhanged19fc32017-10-19 13:06:22 -0700929 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700930 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100931 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700932 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 }
Colin Cross6510f912017-11-29 00:27:14 -0800934 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700935 // Override the -g flag passed globally to remove local variable debug info to reduce
936 // disk and memory usage.
937 javacFlags = append(javacFlags, "-g:source,lines")
938 }
Colin Cross64162712017-08-08 13:17:59 -0700939
Colin Cross66548102018-06-19 22:47:35 -0700940 if ctx.Config().RunErrorProne() {
941 if config.ErrorProneClasspath == nil {
942 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
943 }
944
945 errorProneFlags := []string{
946 "-Xplugin:ErrorProne",
947 "${config.ErrorProneChecks}",
948 }
949 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
950
951 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
952 "'" + strings.Join(errorProneFlags, " ") + "'"
953 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800954 }
955
Nan Zhanged19fc32017-10-19 13:06:22 -0700956 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800957 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
958 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700959 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800960
Colin Crossbe9cdb82019-01-21 21:37:16 -0800961 flags.processor = strings.Join(deps.processorClasses, ",")
962
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100963 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100964 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800965 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
966 // Give host-side tools a version of OpenJDK's standard libraries
967 // close to what they're targeting. As of Dec 2017, AOSP is only
968 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
969 //
970 // When building with OpenJDK 8, the following should have no
971 // effect since those jars would be available by default.
972 //
973 // When building with OpenJDK 9 but targeting a version < 1.8,
974 // putting them on the bootclasspath means that:
975 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
976 // b) references to existing APIs are not reinterpreted in an
977 // OpenJDK 9-specific way, eg. calls to subclasses of
978 // java.nio.Buffer as in http://b/70862583
979 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
980 flags.bootClasspath = append(flags.bootClasspath,
981 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
982 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800983 if Bool(j.properties.Use_tools_jar) {
984 flags.bootClasspath = append(flags.bootClasspath,
985 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
986 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800987 }
988
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100989 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800990 // Manually specify build directory in case it is not under the repo root.
991 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
992 // just adding a symlink under the root doesn't help.)
993 patchPaths := ".:" + ctx.Config().BuildDir()
994 classPath := flags.classpath.FormJavaClassPath("")
995 if classPath != "" {
996 patchPaths += ":" + classPath
997 }
998 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700999 }
1000
Nan Zhanged19fc32017-10-19 13:06:22 -07001001 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001002 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001003
Nan Zhanged19fc32017-10-19 13:06:22 -07001004 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001005 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001006
Colin Cross81440082018-08-15 20:21:55 -07001007 if len(javacFlags) > 0 {
1008 // optimization.
1009 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1010 flags.javacFlags = "$javacFlags"
1011 }
1012
Nan Zhanged19fc32017-10-19 13:06:22 -07001013 return flags
1014}
Colin Crossc0b06f12015-04-08 13:03:43 -07001015
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001016func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1017
Colin Crossebe1a512017-11-14 13:12:14 -08001018 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001019
1020 deps := j.collectDeps(ctx)
1021 flags := j.collectBuilderFlags(ctx, deps)
1022
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001023 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001024 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1025 }
Colin Cross8a497952019-03-05 22:25:09 -08001026 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001027 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001028 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001029 }
1030
Colin Crossaf050172017-11-15 23:01:59 -08001031 srcFiles = j.genSources(ctx, srcFiles, flags)
1032
1033 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001034 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001035 if aaptSrcJar != nil {
1036 srcJars = append(srcJars, aaptSrcJar)
1037 }
Colin Crossb7a63242015-04-16 14:09:14 -07001038
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001039 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1040 // that IDEInfo struct will use
1041 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1042
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001043 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001044 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001045 }
1046
Colin Cross1ee23172017-10-18 14:44:18 -07001047 jarName := ctx.ModuleName() + ".jar"
1048
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001049 javaSrcFiles := srcFiles.FilterByExt(".java")
1050 var uniqueSrcFiles android.Paths
1051 set := make(map[string]bool)
1052 for _, v := range javaSrcFiles {
1053 if _, found := set[v.String()]; !found {
1054 set[v.String()] = true
1055 uniqueSrcFiles = append(uniqueSrcFiles, v)
1056 }
1057 }
1058
Colin Cross55f63ea2018-08-27 12:37:09 -07001059 var kotlinJars android.Paths
1060
Colin Cross93e85952017-08-15 13:34:18 -07001061 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001062 // user defined kotlin flags.
1063 kotlincFlags := j.properties.Kotlincflags
1064 CheckKotlincFlags(ctx, kotlincFlags)
1065
Colin Cross93e85952017-08-15 13:34:18 -07001066 // If there are kotlin files, compile them first but pass all the kotlin and java files
1067 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1068 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001069 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001070 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001071 kotlincFlags = append(kotlincFlags, "-no-jdk")
1072 }
1073 if len(kotlincFlags) > 0 {
1074 // optimization.
1075 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1076 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001077 }
1078
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001079 var kotlinSrcFiles android.Paths
1080 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1081 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1082
Colin Crossafbb1732019-01-17 15:42:52 -08001083 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1084 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1085
1086 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1087 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1088
1089 if len(flags.processorPath) > 0 {
1090 // Use kapt for annotation processing
1091 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1092 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1093 srcJars = append(srcJars, kaptSrcJar)
1094 // Disable annotation processing in javac, it's already been handled by kapt
1095 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001096 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001097 }
Colin Cross93e85952017-08-15 13:34:18 -07001098
Colin Cross1ee23172017-10-18 14:44:18 -07001099 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001100 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001101 if ctx.Failed() {
1102 return
1103 }
1104
1105 // Make javac rule depend on the kotlinc rule
1106 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001107
Colin Cross93e85952017-08-15 13:34:18 -07001108 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001109 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001110 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001111 }
1112
Colin Cross55f63ea2018-08-27 12:37:09 -07001113 jars := append(android.Paths(nil), kotlinJars...)
1114
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001115 // Store the list of .java files that was passed to javac
1116 j.compiledJavaSrcs = uniqueSrcFiles
1117 j.compiledSrcJars = srcJars
1118
Nan Zhang61eaedb2017-11-02 13:28:15 -07001119 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001120 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001121 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1122 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001123 // Formerly, there was a check here that prevented annotation processors
1124 // from being used when sharding was enabled, as some annotation processors
1125 // do not function correctly in sharded environments. It was removed to
1126 // allow for the use of annotation processors that do function correctly
1127 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001128 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001129 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001130 if ctx.Failed() {
1131 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001132 }
1133 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001134 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001135 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001136 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001137 // If error-prone is enabled, add an additional rule to compile the java files into
1138 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001139 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001140 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1141 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001142 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001143 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001144 extraJarDeps = append(extraJarDeps, errorprone)
1145 }
1146
Nan Zhang61eaedb2017-11-02 13:28:15 -07001147 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001148 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001149 shardSize := int(*(j.properties.Javac_shard_size))
1150 var shardSrcs []android.Paths
1151 if len(uniqueSrcFiles) > 0 {
1152 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1153 for idx, shardSrc := range shardSrcs {
1154 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1155 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1156 jars = append(jars, classes)
1157 }
1158 }
1159 if len(srcJars) > 0 {
1160 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1161 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1162 jars = append(jars, classes)
1163 }
1164 } else {
1165 classes := android.PathForModuleOut(ctx, "javac", jarName)
1166 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1167 jars = append(jars, classes)
1168 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001169 if ctx.Config().EmitXrefRules() {
1170 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1171 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1172 j.kytheFiles = append(j.kytheFiles, extractionFile)
1173
1174 }
Colin Crossd6891432017-09-27 17:39:56 -07001175 if ctx.Failed() {
1176 return
1177 }
Colin Cross2fe66872015-03-30 17:20:39 -07001178 }
1179
Colin Cross0c4ce212019-05-03 15:28:19 -07001180 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1181
1182 var includeSrcJar android.WritablePath
1183 if Bool(j.properties.Include_srcs) {
1184 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1185 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1186 }
1187
Colin Crosscedd4762018-09-13 11:26:19 -07001188 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1189 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001190 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001191 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001192
1193 var resArgs []string
1194 var resDeps android.Paths
1195
1196 resArgs = append(resArgs, dirArgs...)
1197 resDeps = append(resDeps, dirDeps...)
1198
1199 resArgs = append(resArgs, fileArgs...)
1200 resDeps = append(resDeps, fileDeps...)
1201
Colin Cross988708c2019-05-06 14:04:11 -07001202 resArgs = append(resArgs, extraArgs...)
1203 resDeps = append(resDeps, extraDeps...)
1204
Colin Cross40a36712017-09-27 17:41:35 -07001205 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001206 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001207 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001208 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001209 if ctx.Failed() {
1210 return
1211 }
1212 }
1213
Colin Cross0c4ce212019-05-03 15:28:19 -07001214 var resourceJars android.Paths
1215 if j.resourceJar != nil {
1216 resourceJars = append(resourceJars, j.resourceJar)
1217 }
1218 if Bool(j.properties.Include_srcs) {
1219 resourceJars = append(resourceJars, includeSrcJar)
1220 }
1221 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001222
Colin Cross0c4ce212019-05-03 15:28:19 -07001223 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001224 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001225 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001226 false, nil, nil)
1227 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001228 } else if len(resourceJars) == 1 {
1229 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001230 }
1231
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001232 if len(deps.staticJars) > 0 {
1233 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001234 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001235
Colin Cross094054a2018-10-17 15:10:48 -07001236 manifest := j.overrideManifest
1237 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001238 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001239 }
Colin Cross635acc92017-09-12 22:50:46 -07001240
Colin Cross8a497952019-03-05 22:25:09 -08001241 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001242 if len(services) > 0 {
1243 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1244 var zipargs []string
1245 for _, file := range services {
1246 serviceFile := file.String()
1247 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1248 }
1249 ctx.Build(pctx, android.BuildParams{
1250 Rule: zip,
1251 Output: servicesJar,
1252 Implicits: services,
1253 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001254 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001255 },
1256 })
1257 jars = append(jars, servicesJar)
1258 }
1259
Colin Cross0a6e0072017-08-30 14:24:55 -07001260 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001261 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001262 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001263
1264 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001265 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1266 // Optimization: skip the combine step if there is nothing to do
1267 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1268 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1269 // any if len(jars) == 1.
1270 outputFile = moduleOutPath
1271 } else {
1272 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1273 ctx.Build(pctx, android.BuildParams{
1274 Rule: android.Cp,
1275 Input: jars[0],
1276 Output: combinedJar,
1277 })
1278 outputFile = combinedJar
1279 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001280 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001281 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001282 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001283 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001284 outputFile = combinedJar
1285 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001286
Colin Cross331a1212018-08-15 20:40:52 -07001287 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001288 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001289 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001290 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001291 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001292 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001293
1294 // jarjar resource jar if necessary
1295 if j.resourceJar != nil {
1296 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001297 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001298 j.resourceJar = resourceJarJarFile
1299 }
1300
Colin Cross0a6e0072017-08-30 14:24:55 -07001301 if ctx.Failed() {
1302 return
1303 }
1304 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001305
1306 // Check package restrictions if necessary.
1307 if len(j.properties.Permitted_packages) > 0 {
1308 // Check packages and copy to package-checked file.
1309 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1310 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1311 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1312
1313 if ctx.Failed() {
1314 return
1315 }
1316 }
1317
Nan Zhanged19fc32017-10-19 13:06:22 -07001318 j.implementationJarFile = outputFile
1319 if j.headerJarFile == nil {
1320 j.headerJarFile = j.implementationJarFile
1321 }
Colin Cross2fe66872015-03-30 17:20:39 -07001322
Colin Cross6510f912017-11-29 00:27:14 -08001323 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001324 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1325 j.properties.Instrument = true
1326 }
1327 }
1328
Colin Cross3144dfc2018-01-03 15:06:47 -08001329 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001330 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1331 }
1332
Colin Cross331a1212018-08-15 20:40:52 -07001333 // merge implementation jar with resources if necessary
1334 implementationAndResourcesJar := outputFile
1335 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001336 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001337 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001338 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001339 false, nil, nil)
1340 implementationAndResourcesJar = combinedJar
1341 }
1342
1343 j.implementationAndResourcesJar = implementationAndResourcesJar
1344
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001345 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001346 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001347 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001348 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001349 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001350 if ctx.Failed() {
1351 return
1352 }
Colin Cross331a1212018-08-15 20:40:52 -07001353
Jiyong Park09cb6292019-07-15 15:29:23 +09001354 // Hidden API CSV generation and dex encoding
1355 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1356 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001357
Colin Cross331a1212018-08-15 20:40:52 -07001358 // merge dex jar with resources if necessary
1359 if j.resourceJar != nil {
1360 jars := android.Paths{dexOutputFile, j.resourceJar}
1361 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1362 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1363 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001364 if j.deviceProperties.UncompressDex {
1365 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1366 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1367 dexOutputFile = combinedAlignedJar
1368 } else {
1369 dexOutputFile = combinedJar
1370 }
Colin Cross331a1212018-08-15 20:40:52 -07001371 }
1372
1373 j.dexJarFile = dexOutputFile
1374
Colin Cross8faf8fc2019-01-16 15:15:52 -08001375 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001376 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1377
1378 j.maybeStrippedDexJarFile = dexOutputFile
1379
Colin Cross3063b782018-08-15 11:19:12 -07001380 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001381
1382 if ctx.Failed() {
1383 return
1384 }
Colin Cross331a1212018-08-15 20:40:52 -07001385 } else {
1386 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001387 }
Colin Cross331a1212018-08-15 20:40:52 -07001388
Colin Crossb7a63242015-04-16 14:09:14 -07001389 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001390
1391 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1392 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001393}
1394
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001395// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1396// since some of these flags may be used internally.
1397func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1398 for _, flag := range flags {
1399 flag = strings.TrimSpace(flag)
1400
1401 if !strings.HasPrefix(flag, "-") {
1402 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1403 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1404 ctx.PropertyErrorf("kotlincflags",
1405 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1406 } else if inList(flag, config.KotlincIllegalFlags) {
1407 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1408 } else if flag == "-include-runtime" {
1409 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1410 } else {
1411 args := strings.Split(flag, " ")
1412 if args[0] == "-kotlin-home" {
1413 ctx.PropertyErrorf("kotlincflags",
1414 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1415 }
1416 }
1417 }
1418}
1419
Colin Cross8eadbf02017-10-24 17:46:00 -07001420func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001421 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001422
1423 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001424 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001425 // Compile java sources into turbine.jar.
1426 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1427 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1428 if ctx.Failed() {
1429 return nil
1430 }
1431 jars = append(jars, turbineJar)
1432 }
1433
Colin Cross55f63ea2018-08-27 12:37:09 -07001434 jars = append(jars, extraJars...)
1435
Nan Zhanged19fc32017-10-19 13:06:22 -07001436 // Combine any static header libraries into classes-header.jar. If there is only
1437 // one input jar this step will be skipped.
1438 var headerJar android.Path
1439 jars = append(jars, deps.staticHeaderJars...)
1440
Colin Cross5c6ecc12017-10-23 18:12:27 -07001441 // we cannot skip the combine step for now if there is only one jar
1442 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1443 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001444 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001445 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001446 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001447
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001448 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001449 // Transform classes.jar into classes-jarjar.jar
1450 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001451 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001452 headerJar = jarjarFile
1453 if ctx.Failed() {
1454 return nil
1455 }
1456 }
1457
1458 return headerJar
1459}
1460
Colin Crosscb933592017-11-22 13:49:43 -08001461func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001462 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001463
Colin Cross7a3139e2017-12-19 13:57:50 -08001464 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001465
Colin Cross84c38822018-01-03 15:59:46 -08001466 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001467 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1468
1469 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1470
1471 j.jacocoReportClassesFile = jacocoReportClassesFile
1472
1473 return instrumentedJar
1474}
1475
albaltai36ff7dc2018-12-25 14:35:23 +08001476var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001477
Nan Zhanged19fc32017-10-19 13:06:22 -07001478func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001479 if j.headerJarFile == nil {
1480 return nil
1481 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001482 return android.Paths{j.headerJarFile}
1483}
1484
1485func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001486 if j.implementationJarFile == nil {
1487 return nil
1488 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001489 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001490}
1491
Colin Crossf24a22a2019-01-31 14:12:44 -08001492func (j *Module) DexJar() android.Path {
1493 return j.dexJarFile
1494}
1495
Colin Cross331a1212018-08-15 20:40:52 -07001496func (j *Module) ResourceJars() android.Paths {
1497 if j.resourceJar == nil {
1498 return nil
1499 }
1500 return android.Paths{j.resourceJar}
1501}
1502
1503func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001504 if j.implementationAndResourcesJar == nil {
1505 return nil
1506 }
Colin Cross331a1212018-08-15 20:40:52 -07001507 return android.Paths{j.implementationAndResourcesJar}
1508}
1509
Colin Cross46c9b8b2017-06-22 16:51:17 -07001510func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001511 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001512 return j.exportAidlIncludeDirs
1513}
1514
Jiyong Park1be96912018-05-28 18:02:19 +09001515func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001516 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001517 return j.exportedSdkLibs
1518}
1519
Colin Cross0c4ce212019-05-03 15:28:19 -07001520func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1521 return j.srcJarArgs, j.srcJarDeps
1522}
1523
Colin Cross46c9b8b2017-06-22 16:51:17 -07001524var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001525
Colin Cross46c9b8b2017-06-22 16:51:17 -07001526func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001527 return j.logtagsSrcs
1528}
1529
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001530// Collect information for opening IDE project files in java/jdeps.go.
1531func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1532 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1533 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001534 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001535 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001536 if j.expandJarjarRules != nil {
1537 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001538 }
1539}
1540
1541func (j *Module) CompilerDeps() []string {
1542 jdeps := []string{}
1543 jdeps = append(jdeps, j.properties.Libs...)
1544 jdeps = append(jdeps, j.properties.Static_libs...)
1545 return jdeps
1546}
1547
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001548func (j *Module) hasCode(ctx android.ModuleContext) bool {
1549 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1550 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1551}
1552
Colin Cross2fe66872015-03-30 17:20:39 -07001553//
1554// Java libraries (.jar file)
1555//
1556
Colin Crossf506d872017-07-19 15:53:04 -07001557type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001558 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001559}
1560
Colin Cross42be7612019-02-21 18:12:14 -08001561func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001562 // Store uncompressed (and do not strip) dex files from boot class path jars.
1563 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1564 return true
1565 }
1566
1567 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001568 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001569 return true
1570 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001571 if ctx.Config().UncompressPrivAppDex() &&
1572 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1573 return true
1574 }
1575
Colin Cross2fc72f62018-12-21 12:59:54 -08001576 return false
1577}
1578
Colin Crossf506d872017-07-19 15:53:04 -07001579func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001580 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1581 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001582 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001583 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001584 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001585 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001586
Jiyong Park7f7766d2019-07-25 22:02:35 +09001587 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1588 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001589 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1590 ctx.ModuleName()+".jar", j.outputFile)
1591 }
Colin Crossb7a63242015-04-16 14:09:14 -07001592}
1593
Colin Crossf506d872017-07-19 15:53:04 -07001594func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001595 j.deps(ctx)
1596}
1597
Colin Cross1b16b0e2019-02-12 14:41:32 -08001598// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1599//
1600// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1601// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1602// as a `static_libs` dependency of another module.
1603//
1604// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1605// a device.
1606//
1607// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1608// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001609func LibraryFactory() android.Module {
1610 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001611
Colin Cross9ae1b922018-06-26 17:59:05 -07001612 module.AddProperties(
1613 &module.Module.properties,
1614 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001615 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001616 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001617
Colin Cross9ae1b922018-06-26 17:59:05 -07001618 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001619 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001620 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001621}
1622
Colin Cross1b16b0e2019-02-12 14:41:32 -08001623// java_library_static is an obsolete alias for java_library.
1624func LibraryStaticFactory() android.Module {
1625 return LibraryFactory()
1626}
1627
1628// java_library_host builds and links sources into a `.jar` file for the host.
1629//
1630// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1631// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001632func LibraryHostFactory() android.Module {
1633 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001634
Colin Cross6af17aa2017-09-20 12:59:05 -07001635 module.AddProperties(
1636 &module.Module.properties,
1637 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001638
Colin Cross9ae1b922018-06-26 17:59:05 -07001639 module.Module.properties.Installable = proptools.BoolPtr(true)
1640
Colin Cross89536d42017-07-07 14:35:50 -07001641 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001642 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001643 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001644}
1645
1646//
Colin Crossb628ea52018-08-14 16:42:33 -07001647// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001648//
1649
1650type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001651 // list of compatibility suites (for example "cts", "vts") that the module should be
1652 // installed into.
1653 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001654
1655 // the name of the test configuration (for example "AndroidTest.xml") that should be
1656 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001657 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001658
Jack He33338892018-09-19 02:21:28 -07001659 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1660 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001661 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001662
Colin Crossd96ca352018-08-10 16:06:24 -07001663 // list of files or filegroup modules that provide data that should be installed alongside
1664 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001665 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001666}
1667
Paul Duffin42df1442019-03-20 12:45:53 +00001668type testHelperLibraryProperties struct {
1669 // list of compatibility suites (for example "cts", "vts") that the module should be
1670 // installed into.
1671 Test_suites []string `android:"arch_variant"`
1672}
1673
Colin Cross05638fc2018-04-09 18:40:24 -07001674type Test struct {
1675 Library
1676
1677 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001678
1679 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001680 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001681}
1682
Paul Duffin42df1442019-03-20 12:45:53 +00001683type TestHelperLibrary struct {
1684 Library
1685
1686 testHelperLibraryProperties testHelperLibraryProperties
1687}
1688
Colin Cross303e21f2018-08-07 16:49:25 -07001689func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001690 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 -08001691 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001692
1693 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001694}
1695
Paul Duffin42df1442019-03-20 12:45:53 +00001696func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1697 j.Library.GenerateAndroidBuildActions(ctx)
1698}
1699
Colin Cross1b16b0e2019-02-12 14:41:32 -08001700// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1701// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1702//
1703// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1704// compiled against the device bootclasspath.
1705//
1706// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1707// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001708func TestFactory() android.Module {
1709 module := &Test{}
1710
1711 module.AddProperties(
1712 &module.Module.properties,
1713 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001714 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001715 &module.Module.protoProperties,
1716 &module.testProperties)
1717
Colin Cross9ae1b922018-06-26 17:59:05 -07001718 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001719 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001720
Colin Cross05638fc2018-04-09 18:40:24 -07001721 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001722 return module
1723}
1724
Paul Duffin42df1442019-03-20 12:45:53 +00001725// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1726func TestHelperLibraryFactory() android.Module {
1727 module := &TestHelperLibrary{}
1728
1729 module.AddProperties(
1730 &module.Module.properties,
1731 &module.Module.deviceProperties,
1732 &module.Module.dexpreoptProperties,
1733 &module.Module.protoProperties,
1734 &module.testHelperLibraryProperties)
1735
Colin Cross9a4abed2019-04-24 13:19:28 -07001736 module.Module.properties.Installable = proptools.BoolPtr(true)
1737 module.Module.dexpreopter.isTest = true
1738
Paul Duffin42df1442019-03-20 12:45:53 +00001739 InitJavaModule(module, android.HostAndDeviceSupported)
1740 return module
1741}
1742
Colin Cross1b16b0e2019-02-12 14:41:32 -08001743// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1744// allow running the test with `atest` or a `TEST_MAPPING` file.
1745//
1746// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1747// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001748func TestHostFactory() android.Module {
1749 module := &Test{}
1750
1751 module.AddProperties(
1752 &module.Module.properties,
1753 &module.Module.protoProperties,
1754 &module.testProperties)
1755
Colin Cross9ae1b922018-06-26 17:59:05 -07001756 module.Module.properties.Installable = proptools.BoolPtr(true)
1757
Colin Cross05638fc2018-04-09 18:40:24 -07001758 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001759 return module
1760}
1761
1762//
Colin Cross2fe66872015-03-30 17:20:39 -07001763// Java Binaries (.jar file plus wrapper script)
1764//
1765
Colin Crossf506d872017-07-19 15:53:04 -07001766type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001767 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001768 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001769
1770 // Name of the class containing main to be inserted into the manifest as Main-Class.
1771 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001772}
1773
Colin Crossf506d872017-07-19 15:53:04 -07001774type Binary struct {
1775 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001776
Colin Crossf506d872017-07-19 15:53:04 -07001777 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001778
Colin Cross6b4a32d2017-12-05 13:42:45 -08001779 isWrapperVariant bool
1780
Colin Crossc3315992017-12-08 19:12:36 -08001781 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001782 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001783}
1784
Alex Light24237172017-10-26 09:46:21 -07001785func (j *Binary) HostToolPath() android.OptionalPath {
1786 return android.OptionalPathForPath(j.binaryFile)
1787}
1788
Colin Crossf506d872017-07-19 15:53:04 -07001789func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001790 if ctx.Arch().ArchType == android.Common {
1791 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001792 if j.binaryProperties.Main_class != nil {
1793 if j.properties.Manifest != nil {
1794 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1795 }
1796 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1797 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1798 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1799 }
1800
Colin Cross6b4a32d2017-12-05 13:42:45 -08001801 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001802 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001803 // Handle the binary wrapper
1804 j.isWrapperVariant = true
1805
Colin Cross366938f2017-12-11 16:29:02 -08001806 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001807 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001808 } else {
1809 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1810 }
1811
1812 // Depend on the installed jar so that the wrapper doesn't get executed by
1813 // another build rule before the jar has been installed.
1814 jarFile := ctx.PrimaryModule().(*Binary).installFile
1815
1816 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1817 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001818 }
Colin Cross2fe66872015-03-30 17:20:39 -07001819}
1820
Colin Crossf506d872017-07-19 15:53:04 -07001821func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001822 if ctx.Arch().ArchType == android.Common {
1823 j.deps(ctx)
1824 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001825}
1826
Colin Cross1b16b0e2019-02-12 14:41:32 -08001827// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1828// as well.
1829//
1830// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1831// compiled against the device bootclasspath.
1832//
1833// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1834// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001835func BinaryFactory() android.Module {
1836 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001837
Colin Cross36242852017-06-23 15:06:31 -07001838 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001839 &module.Module.properties,
1840 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001841 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001842 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001843 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001844
Colin Cross9ae1b922018-06-26 17:59:05 -07001845 module.Module.properties.Installable = proptools.BoolPtr(true)
1846
Colin Cross6b4a32d2017-12-05 13:42:45 -08001847 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1848 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001849 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001850}
1851
Colin Cross1b16b0e2019-02-12 14:41:32 -08001852// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1853//
1854// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1855// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001856func BinaryHostFactory() android.Module {
1857 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001858
Colin Cross36242852017-06-23 15:06:31 -07001859 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001860 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001861 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001862 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001863
Colin Cross9ae1b922018-06-26 17:59:05 -07001864 module.Module.properties.Installable = proptools.BoolPtr(true)
1865
Colin Cross6b4a32d2017-12-05 13:42:45 -08001866 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1867 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001868 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001869}
1870
1871//
1872// Java prebuilts
1873//
1874
Colin Cross74d73e22017-08-02 11:05:49 -07001875type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001876 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001877
Nan Zhangea568a42017-11-08 21:20:04 -08001878 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001879
1880 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001881
1882 // List of shared java libs that this module has dependencies to
1883 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001884
1885 // List of files to remove from the jar file(s)
1886 Exclude_files []string
1887
1888 // List of directories to remove from the jar file(s)
1889 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001890
1891 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001892 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001893}
1894
1895type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001896 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001897 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001898 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001899 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001900
Colin Cross74d73e22017-08-02 11:05:49 -07001901 properties ImportProperties
1902
Colin Cross0a6e0072017-08-30 14:24:55 -07001903 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001904 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001905}
1906
Colin Cross83bb3162018-06-25 15:48:06 -07001907func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001908 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001909}
1910
1911func (j *Import) minSdkVersion() string {
1912 return j.sdkVersion()
1913}
1914
Colin Cross74d73e22017-08-02 11:05:49 -07001915func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001916 return &j.prebuilt
1917}
1918
Colin Cross74d73e22017-08-02 11:05:49 -07001919func (j *Import) PrebuiltSrcs() []string {
1920 return j.properties.Jars
1921}
1922
1923func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001924 return j.prebuilt.Name(j.ModuleBase.Name())
1925}
1926
Colin Cross74d73e22017-08-02 11:05:49 -07001927func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001928 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001929}
1930
Colin Cross74d73e22017-08-02 11:05:49 -07001931func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001932 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001933
Nan Zhang4c819fb2018-08-27 18:31:46 -07001934 jarName := ctx.ModuleName() + ".jar"
1935 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001936 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1937 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001938 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001939 inputFile := outputFile
1940 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1941 TransformJetifier(ctx, outputFile, inputFile)
1942 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001943 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001944
1945 ctx.VisitDirectDeps(func(module android.Module) {
1946 otherName := ctx.OtherModuleName(module)
1947 tag := ctx.OtherModuleDependencyTag(module)
1948
1949 switch dep := module.(type) {
1950 case Dependency:
1951 switch tag {
1952 case libTag, staticLibTag:
1953 // sdk lib names from dependencies are re-exported
1954 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1955 }
1956 case SdkLibraryDependency:
1957 switch tag {
1958 case libTag:
1959 // names of sdk libs that are directly depended are exported
1960 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1961 }
1962 }
1963 })
1964
1965 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001966 if Bool(j.properties.Installable) {
1967 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1968 ctx.ModuleName()+".jar", outputFile)
1969 }
Colin Cross2fe66872015-03-30 17:20:39 -07001970}
1971
Colin Cross74d73e22017-08-02 11:05:49 -07001972var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001973
Nan Zhanged19fc32017-10-19 13:06:22 -07001974func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001975 if j.combinedClasspathFile == nil {
1976 return nil
1977 }
Colin Cross37f6d792018-07-12 12:28:41 -07001978 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001979}
1980
1981func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001982 if j.combinedClasspathFile == nil {
1983 return nil
1984 }
Colin Cross37f6d792018-07-12 12:28:41 -07001985 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001986}
1987
Colin Cross331a1212018-08-15 20:40:52 -07001988func (j *Import) ResourceJars() android.Paths {
1989 return nil
1990}
1991
1992func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001993 if j.combinedClasspathFile == nil {
1994 return nil
1995 }
Colin Cross331a1212018-08-15 20:40:52 -07001996 return android.Paths{j.combinedClasspathFile}
1997}
1998
Colin Crossf24a22a2019-01-31 14:12:44 -08001999func (j *Import) DexJar() android.Path {
2000 return nil
2001}
2002
Colin Cross74d73e22017-08-02 11:05:49 -07002003func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002004 return nil
2005}
2006
Jiyong Park1be96912018-05-28 18:02:19 +09002007func (j *Import) ExportedSdkLibs() []string {
2008 return j.exportedSdkLibs
2009}
2010
Colin Cross0c4ce212019-05-03 15:28:19 -07002011func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2012 return nil, nil
2013}
2014
albaltai36ff7dc2018-12-25 14:35:23 +08002015// Add compile time check for interface implementation
2016var _ android.IDEInfo = (*Import)(nil)
2017var _ android.IDECustomizedModuleName = (*Import)(nil)
2018
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002019// Collect information for opening IDE project files in java/jdeps.go.
2020const (
2021 removedPrefix = "prebuilt_"
2022)
2023
2024func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2025 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2026}
2027
2028func (j *Import) IDECustomizedModuleName() string {
2029 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2030 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2031 // solution to get the Import name.
2032 name := j.Name()
2033 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002034 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002035 }
2036 return name
2037}
2038
Colin Cross74d73e22017-08-02 11:05:49 -07002039var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002040
Colin Cross1b16b0e2019-02-12 14:41:32 -08002041// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2042//
2043// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2044// compiled against an Android classpath.
2045//
2046// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2047// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002048func ImportFactory() android.Module {
2049 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002050
Colin Cross74d73e22017-08-02 11:05:49 -07002051 module.AddProperties(&module.properties)
2052
2053 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002054 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002055 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002056 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002057}
2058
Colin Cross1b16b0e2019-02-12 14:41:32 -08002059// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2060// module.
2061//
2062// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2063// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002064func ImportFactoryHost() android.Module {
2065 module := &Import{}
2066
2067 module.AddProperties(&module.properties)
2068
2069 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002070 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002071 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002072 return module
2073}
2074
Colin Cross42be7612019-02-21 18:12:14 -08002075// dex_import module
2076
2077type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002078 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002079}
2080
2081type DexImport struct {
2082 android.ModuleBase
2083 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002084 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002085 prebuilt android.Prebuilt
2086
2087 properties DexImportProperties
2088
2089 dexJarFile android.Path
2090 maybeStrippedDexJarFile android.Path
2091
2092 dexpreopter
2093}
2094
2095func (j *DexImport) Prebuilt() *android.Prebuilt {
2096 return &j.prebuilt
2097}
2098
2099func (j *DexImport) PrebuiltSrcs() []string {
2100 return j.properties.Jars
2101}
2102
2103func (j *DexImport) Name() string {
2104 return j.prebuilt.Name(j.ModuleBase.Name())
2105}
2106
Colin Cross42be7612019-02-21 18:12:14 -08002107func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2108 if len(j.properties.Jars) != 1 {
2109 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2110 }
2111
2112 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2113 j.dexpreopter.isInstallable = true
2114 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2115
2116 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2117 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2118
2119 if j.dexpreopter.uncompressedDex {
2120 rule := android.NewRuleBuilder()
2121
2122 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2123 rule.Temporary(temporary)
2124
2125 // use zip2zip to uncompress classes*.dex files
2126 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002127 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002128 FlagWithInput("-i ", inputJar).
2129 FlagWithOutput("-o ", temporary).
2130 FlagWithArg("-0 ", "'classes*.dex'")
2131
2132 // use zipalign to align uncompressed classes*.dex files
2133 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002134 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002135 Flag("-f").
2136 Text("4").
2137 Input(temporary).
2138 Output(dexOutputFile)
2139
2140 rule.DeleteTemporaryFiles()
2141
2142 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2143 } else {
2144 ctx.Build(pctx, android.BuildParams{
2145 Rule: android.Cp,
2146 Input: inputJar,
2147 Output: dexOutputFile,
2148 })
2149 }
2150
2151 j.dexJarFile = dexOutputFile
2152
2153 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2154
2155 j.maybeStrippedDexJarFile = dexOutputFile
2156
2157 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2158 ctx.ModuleName()+".jar", dexOutputFile)
2159}
2160
2161func (j *DexImport) DexJar() android.Path {
2162 return j.dexJarFile
2163}
2164
2165// dex_import imports a `.jar` file containing classes.dex files.
2166//
2167// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2168// to the device.
2169func DexImportFactory() android.Module {
2170 module := &DexImport{}
2171
2172 module.AddProperties(&module.properties)
2173
2174 android.InitPrebuiltModule(module, &module.properties.Jars)
2175 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002176 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002177 return module
2178}
2179
Colin Cross89536d42017-07-07 14:35:50 -07002180//
2181// Defaults
2182//
2183type Defaults struct {
2184 android.ModuleBase
2185 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002186 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002187}
2188
Colin Cross1b16b0e2019-02-12 14:41:32 -08002189// java_defaults provides a set of properties that can be inherited by other java or android modules.
2190//
2191// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2192// property in the defaults module that exists in the depending module will be prepended to the depending module's
2193// value for that property.
2194//
2195// Example:
2196//
2197// java_defaults {
2198// name: "example_defaults",
2199// srcs: ["common/**/*.java"],
2200// javacflags: ["-Xlint:all"],
2201// aaptflags: ["--auto-add-overlay"],
2202// }
2203//
2204// java_library {
2205// name: "example",
2206// defaults: ["example_defaults"],
2207// srcs: ["example/**/*.java"],
2208// }
2209//
2210// is functionally identical to:
2211//
2212// java_library {
2213// name: "example",
2214// srcs: [
2215// "common/**/*.java",
2216// "example/**/*.java",
2217// ],
2218// javacflags: ["-Xlint:all"],
2219// }
Colin Cross89536d42017-07-07 14:35:50 -07002220func defaultsFactory() android.Module {
2221 return DefaultsFactory()
2222}
2223
2224func DefaultsFactory(props ...interface{}) android.Module {
2225 module := &Defaults{}
2226
2227 module.AddProperties(props...)
2228 module.AddProperties(
2229 &CompilerProperties{},
2230 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002231 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002232 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002233 &aaptProperties{},
2234 &androidLibraryProperties{},
2235 &appProperties{},
2236 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002237 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002238 &ImportProperties{},
2239 &AARImportProperties{},
2240 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002241 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002242 )
2243
2244 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002245 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002246 return module
2247}
Nan Zhangea568a42017-11-08 21:20:04 -08002248
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002249func kytheExtractJavaFactory() android.Singleton {
2250 return &kytheExtractJavaSingleton{}
2251}
2252
2253type kytheExtractJavaSingleton struct {
2254}
2255
2256func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2257 var xrefTargets android.Paths
2258 ctx.VisitAllModules(func(module android.Module) {
2259 if javaModule, ok := module.(xref); ok {
2260 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2261 }
2262 })
2263 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2264 if len(xrefTargets) > 0 {
2265 ctx.Build(pctx, android.BuildParams{
2266 Rule: blueprint.Phony,
2267 Output: android.PathForPhony(ctx, "xref_java"),
2268 Inputs: xrefTargets,
2269 })
2270 }
2271}
2272
Nan Zhangea568a42017-11-08 21:20:04 -08002273var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002274var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002275var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002276var inList = android.InList