blob: de78f281920ffa58fd9a4c67eedb49c82187237b [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 Crossfc3674a2017-09-18 17:41:52 -070022 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070023 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070030 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross89536d42017-07-07 14:35:50 -070034 android.RegisterModuleType("java_defaults", defaultsFactory)
35
Colin Crossa60ead82017-10-02 18:10:21 -070036 android.RegisterModuleType("java_library", LibraryFactory(true))
37 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070038 android.RegisterModuleType("java_library_host", LibraryHostFactory)
39 android.RegisterModuleType("java_binary", BinaryFactory)
40 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070041 android.RegisterModuleType("java_import", ImportFactory)
42 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070045}
46
Colin Cross2fe66872015-03-30 17:20:39 -070047// TODO:
48// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070049// Renderscript
50// Post-jar passes:
51// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070052// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070053// DroidDoc
54// Findbugs
55
Colin Cross89536d42017-07-07 14:35:50 -070056type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070057 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
58 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070059 Srcs []string `android:"arch_variant"`
60
61 // list of source files that should not be used to build the Java module.
62 // This is most useful in the arch/multilib variants to remove non-common files
63 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070064
65 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070066 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070067
Colin Cross86a63ff2017-09-27 17:33:10 -070068 // list of directories that should be excluded from java_resource_dirs
69 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070070
Colin Cross0f37af02017-09-27 17:42:05 -070071 // list of files to use as Java resources
72 Java_resources []string `android:"arch_variant"`
73
74 // list of files that should be excluded from java_resources
75 Exclude_java_resources []string `android:"arch_variant"`
76
Colin Crossfa5eb232017-10-01 20:33:03 -070077 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070078 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070079 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070080
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the framework libraries (legacy-test, core-junit,
82 // ext, and framework for device targets)
83 No_framework_libs *bool
84
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000085 // Use renamed kotlin stdlib (com.android.kotlin.*). This allows kotlin usage without colliding
86 // with app-provided kotlin stdlib.
87 Renamed_kotlin_stdlib *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700102 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
107 // If set to false, don't allow this module to be installed. Defaults to true.
108 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700109
Colin Cross0f37af02017-09-27 17:42:05 -0700110 // If set to true, include sources used to compile the module in to the final jar
111 Include_srcs *bool
112
Colin Cross32f676a2017-09-06 13:41:06 -0700113 // List of modules to use as annotation processors
114 Annotation_processors []string
115
116 // List of classes to pass to javac to use as annotation processors
117 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700118
Nan Zhang61eaedb2017-11-02 13:28:15 -0700119 // The number of Java source entries each Javac instance can process
120 Javac_shard_size *int64
121
Nan Zhang5f8cb422018-02-06 10:34:32 -0800122 // Add host jdk tools.jar to bootclasspath
123 Use_tools_jar *bool
124
Colin Cross1369cdb2017-09-29 17:58:17 -0700125 Openjdk9 struct {
126 // List of source files that should only be used when passing -source 1.9
127 Srcs []string
128
129 // List of javac flags that should only be used when passing -source 1.9
130 Javacflags []string
131 }
Colin Crosscb933592017-11-22 13:49:43 -0800132
133 Jacoco struct {
134 // List of classes to include for instrumentation with jacoco to collect coverage
135 // information at runtime when building with coverage enabled. If unset defaults to all
136 // classes.
137 // Supports '*' as the last character of an entry in the list as a wildcard match.
138 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
139 // it matches classes in the package that have the class name as a prefix.
140 Include_filter []string
141
142 // List of classes to exclude from instrumentation with jacoco to collect coverage
143 // information at runtime when building with coverage enabled. Overrides classes selected
144 // by the include_filter property.
145 // Supports '*' as the last character of an entry in the list as a wildcard match.
146 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
147 // it matches classes in the package that have the class name as a prefix.
148 Exclude_filter []string
149 }
150
Andreas Gampef3e5b552018-01-22 21:27:21 -0800151 Errorprone struct {
152 // List of javac flags that should only be used when running errorprone.
153 Javacflags []string
154 }
155
Colin Cross0f2ee152017-12-14 15:22:43 -0800156 Proto struct {
157 // List of extra options that will be passed to the proto generator.
158 Output_params []string
159 }
160
Colin Crosscb933592017-11-22 13:49:43 -0800161 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700162}
163
Colin Cross89536d42017-07-07 14:35:50 -0700164type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700165 // list of module-specific flags that will be used for dex compiles
166 Dxflags []string `android:"arch_variant"`
167
Colin Cross7d5136f2015-05-11 13:39:40 -0700168 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800169 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700170
Colin Crossebe1a512017-11-14 13:12:14 -0800171 Aidl struct {
172 // Top level directories to pass to aidl tool
173 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700174
Colin Crossebe1a512017-11-14 13:12:14 -0800175 // Directories rooted at the Android.bp file to pass to aidl tool
176 Local_include_dirs []string
177
178 // directories that should be added as include directories for any aidl sources of modules
179 // that depend on this module, as well as to aidl for this module.
180 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100181
182 // whether to generate traces (for systrace) for this interface
183 Generate_traces *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800184 }
Colin Cross92430102017-10-09 14:59:32 -0700185
186 // If true, export a copy of the module as a -hostdex module for host testing.
187 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700188
Colin Cross1bd87802017-12-05 15:31:19 -0800189 Dex_preopt struct {
190 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
191 // true.
192 Enabled *bool
193
194 // If true, generate an app image (.art file) for this module.
195 App_image *bool
196
197 // If true, use a checked-in profile to guide optimization. Defaults to false unless
198 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
199 // that matches the name of this module, in which case it is defaulted to true.
200 Profile_guided *bool
201
202 // If set, provides the path to profile relative to the Android.bp file. If not set,
203 // defaults to searching for a file that matches the name of this module in the default
204 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
205 Profile *string
206 }
Colin Crossa22116e2017-10-19 14:18:58 -0700207
Colin Cross66dbc0b2017-12-28 12:23:20 -0800208 Optimize struct {
209 // If false, disable all optimization. Defaults to true for apps, false for
210 // libraries and tests.
211 Enabled *bool
212
213 // If true, optimize for size by removing unused code. Defaults to true for apps,
214 // false for libraries and tests.
215 Shrink *bool
216
217 // If true, optimize bytecode. Defaults to false.
218 Optimize *bool
219
220 // If true, obfuscate bytecode. Defaults to false.
221 Obfuscate *bool
222
223 // If true, do not use the flag files generated by aapt that automatically keep
224 // classes referenced by the app manifest. Defaults to false.
225 No_aapt_flags *bool
226
227 // Flags to pass to proguard.
228 Proguard_flags []string
229
230 // Specifies the locations of files containing proguard flags.
231 Proguard_flags_files []string
232 }
233
Colin Cross1369cdb2017-09-29 17:58:17 -0700234 // When targeting 1.9, override the modules to use with --system
235 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700236}
237
Colin Cross46c9b8b2017-06-22 16:51:17 -0700238// Module contains the properties and members used by all java module types
239type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700240 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700241 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700242
Colin Cross89536d42017-07-07 14:35:50 -0700243 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700244 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700245 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700246
Nan Zhanged19fc32017-10-19 13:06:22 -0700247 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
248 headerJarFile android.Path
249
250 // full implementation jar file suitable for static dependency of another module compile
251 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700252
Colin Cross6ade34f2017-09-15 13:00:47 -0700253 // output file containing classes.dex
254 dexJarFile android.Path
255
Colin Crosscb933592017-11-22 13:49:43 -0800256 // output file containing uninstrumented classes that will be instrumented by jacoco
257 jacocoReportClassesFile android.Path
258
Colin Cross66dbc0b2017-12-28 12:23:20 -0800259 // output file containing mapping of obfuscated names
260 proguardDictionary android.Path
261
Colin Crossb7a63242015-04-16 14:09:14 -0700262 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700263 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700264
Colin Cross635c3b02016-05-18 15:37:25 -0700265 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700266
Colin Cross635c3b02016-05-18 15:37:25 -0700267 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700268
Colin Cross2fe66872015-03-30 17:20:39 -0700269 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700270 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800271
272 // list of .java files and srcjars that was passed to javac
273 compiledJavaSrcs android.Paths
274 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800275
276 // list of extra progurad flag files
277 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700278}
279
Colin Cross54250902017-12-05 09:28:08 -0800280func (j *Module) Srcs() android.Paths {
281 return android.Paths{j.implementationJarFile}
282}
283
284var _ android.SourceFileProducer = (*Module)(nil)
285
Colin Crossf506d872017-07-19 15:53:04 -0700286type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 HeaderJars() android.Paths
288 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700289 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700290}
291
Nan Zhangb2b33de2018-02-23 11:18:47 -0800292type SrcDependency interface {
293 CompiledSrcs() android.Paths
294 CompiledSrcJars() android.Paths
295}
296
297func (j *Module) CompiledSrcs() android.Paths {
298 return j.compiledJavaSrcs
299}
300
301func (j *Module) CompiledSrcJars() android.Paths {
302 return j.compiledSrcJars
303}
304
305var _ SrcDependency = (*Module)(nil)
306
Colin Cross89536d42017-07-07 14:35:50 -0700307func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
308 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
309 android.InitDefaultableModule(module)
310}
311
Colin Crossbe1da472017-07-07 15:59:46 -0700312type dependencyTag struct {
313 blueprint.BaseDependencyTag
314 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700315}
316
Colin Crossbe1da472017-07-07 15:59:46 -0700317var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700318 staticLibTag = dependencyTag{name: "staticlib"}
319 libTag = dependencyTag{name: "javalib"}
320 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700321 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700322 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800323 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700324 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800325 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700326)
Colin Cross2fe66872015-03-30 17:20:39 -0700327
Colin Crossfc3674a2017-09-18 17:41:52 -0700328type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700329 useModule, useFiles, useDefaultLibs, invalidVersion bool
330
Colin Cross1369cdb2017-09-29 17:58:17 -0700331 module string
332 systemModules string
333
334 jar android.Path
335 aidl android.Path
336}
337
338func sdkStringToNumber(ctx android.BaseContext, v string) int {
339 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900340 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900341 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700342 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900343 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700344 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
345 return -1
346 } else {
347 return i
348 }
349 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700350}
351
Colin Cross3144dfc2018-01-03 15:06:47 -0800352func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
353 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
354}
355
356func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
357 return j.shouldInstrument(ctx) &&
358 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
359 ctx.Config().UnbundledBuild())
360}
361
Colin Crossfc3674a2017-09-18 17:41:52 -0700362func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700363 i := sdkStringToNumber(ctx, v)
364 if i == -1 {
365 // Invalid sdk version, error handled by sdkStringToNumber.
366 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700367 }
368
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900369 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
370 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
371 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
372 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
373 if ctx.DeviceSpecific() || ctx.SocSpecific() {
374 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
375 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
376 }
377 }
378 version := strings.TrimPrefix(v, "system_")
379 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
380 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
381 v, allowed_versions)
382 }
383 }
384
Colin Crossfc3674a2017-09-18 17:41:52 -0700385 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900386 isCore := strings.HasPrefix(v, "core_")
387 if isCore {
388 v = strings.TrimPrefix(v, "core_")
389 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700390 dir := filepath.Join("prebuilts/sdk", v)
391 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900392 if isCore {
393 jar = filepath.Join(dir, "core.jar")
394 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700395 aidl := filepath.Join(dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800396 jarPath := android.ExistentPathForSource(ctx, jar)
397 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700398
Colin Cross6510f912017-11-29 00:27:14 -0800399 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700400 return sdkDep{
401 invalidVersion: true,
402 module: "sdk_v" + v,
403 }
404 }
405
Colin Crossfc3674a2017-09-18 17:41:52 -0700406 if !jarPath.Valid() {
407 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
408 return sdkDep{}
409 }
Colin Cross47ff2522017-10-02 14:22:08 -0700410
Colin Crossfc3674a2017-09-18 17:41:52 -0700411 if !aidlPath.Valid() {
412 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
413 return sdkDep{}
414 }
Colin Cross47ff2522017-10-02 14:22:08 -0700415
Colin Crossfc3674a2017-09-18 17:41:52 -0700416 return sdkDep{
417 useFiles: true,
418 jar: jarPath.Path(),
419 aidl: aidlPath.Path(),
420 }
421 }
422
Colin Cross2ebc4762017-10-20 14:00:31 -0700423 //toModule := func(m string) sdkDep {
424 // return sdkDep{
425 // useModule: true,
426 // module: m,
427 // systemModules: m + "_system_modules",
428 // }
429 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700430
Colin Cross6510f912017-11-29 00:27:14 -0800431 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700432 return toFile(v)
433 }
434
435 switch v {
436 case "":
437 return sdkDep{
438 useDefaultLibs: true,
439 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700440 // TODO(ccross): re-enable these once we generate stubs, until then
441 // use the stubs in prebuilts/sdk/*current
442 //case "current":
443 // return toModule("android_stubs_current")
444 //case "system_current":
445 // return toModule("android_system_stubs_current")
446 //case "test_current":
447 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700448 default:
449 return toFile(v)
450 }
451}
452
Colin Crossbe1da472017-07-07 15:59:46 -0700453func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700454 if ctx.Device() {
455 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800456 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700457 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700458 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800459 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700460 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
461 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700462 if !proptools.Bool(j.properties.No_framework_libs) {
463 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
464 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700465 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800466 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
468 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700469 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800470 if Bool(j.deviceProperties.Optimize.Enabled) {
471 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
472 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
473 }
Colin Crossbe1da472017-07-07 15:59:46 -0700474 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700475 } else if j.deviceProperties.System_modules == nil {
476 ctx.PropertyErrorf("no_standard_libs",
477 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
Colin Cross6510f912017-11-29 00:27:14 -0800478 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700479 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700480 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800481 if ctx.ModuleName() == "framework" {
482 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
483 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800484 if ctx.ModuleName() == "android_stubs_current" ||
485 ctx.ModuleName() == "android_system_stubs_current" ||
486 ctx.ModuleName() == "android_test_stubs_current" {
487 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
488 }
Colin Cross2fe66872015-03-30 17:20:39 -0700489 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700490
Colin Crossf506d872017-07-19 15:53:04 -0700491 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
492 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700493 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700494
495 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000496 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700497 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800498 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700499
500 if j.hasSrcExt(".proto") {
501 protoDeps(ctx, &j.protoProperties)
502 }
Colin Cross93e85952017-08-15 13:34:18 -0700503
504 if j.hasSrcExt(".kt") {
505 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
506 // Kotlin files
507 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
508 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800509
510 if j.shouldInstrumentStatic(ctx) {
511 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
512 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700513}
514
515func hasSrcExt(srcs []string, ext string) bool {
516 for _, src := range srcs {
517 if filepath.Ext(src) == ext {
518 return true
519 }
520 }
521
522 return false
523}
524
Nan Zhang61eaedb2017-11-02 13:28:15 -0700525func shardPaths(paths android.Paths, shardSize int) []android.Paths {
526 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
527 for len(paths) > shardSize {
528 ret = append(ret, paths[0:shardSize])
529 paths = paths[shardSize:]
530 }
531 if len(paths) > 0 {
532 ret = append(ret, paths)
533 }
534 return ret
535}
536
Colin Cross6af17aa2017-09-20 12:59:05 -0700537func (j *Module) hasSrcExt(ext string) bool {
538 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700539}
540
Colin Cross46c9b8b2017-06-22 16:51:17 -0700541func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700542 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700543
Colin Crossebe1a512017-11-14 13:12:14 -0800544 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
545 aidlIncludes = append(aidlIncludes,
546 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
547 aidlIncludes = append(aidlIncludes,
548 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700549
550 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700551 if aidlPreprocess.Valid() {
552 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700553 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700554 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700555 }
556
Colin Cross635c3b02016-05-18 15:37:25 -0700557 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800558 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700559 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800560 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700561 flags = append(flags, "-I"+src.String())
562 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700563
Martijn Coeneneab15642018-03-09 09:29:59 +0100564 if Bool(j.deviceProperties.Aidl.Generate_traces) {
565 flags = append(flags, "-t")
566 }
567
Colin Crossf03c82b2015-04-13 13:53:40 -0700568 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700569}
570
Colin Cross32f676a2017-09-06 13:41:06 -0700571type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800572 classpath classpath
573 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700574 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700575 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700576 staticJarResources android.Paths
577 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800578 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700579 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700580 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700581 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700582 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700583}
Colin Cross2fe66872015-03-30 17:20:39 -0700584
Colin Cross54250902017-12-05 09:28:08 -0800585func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
586 for _, f := range dep.Srcs() {
587 if f.Ext() != ".jar" {
588 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
589 ctx.OtherModuleName(dep.(blueprint.Module)))
590 }
591 }
592}
593
Jiyong Park2d492942018-03-05 17:44:10 +0900594type linkType int
595
596const (
597 javaCore linkType = iota
598 javaSdk
599 javaSystem
600 javaPlatform
601)
602
603func getLinkType(m *Module) linkType {
604 ver := String(m.deviceProperties.Sdk_version)
605 if strings.HasPrefix(ver, "core_") {
606 return javaCore
607 } else if strings.HasPrefix(ver, "system_") {
608 return javaSystem
609 } else if _, err := strconv.Atoi(ver); err == nil || ver == "current" {
610 return javaSdk
611 } else {
612 // test_current falls back here as well
613 return javaPlatform
614 }
615}
616
Jiyong Park750e5572018-01-31 00:20:13 +0900617func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Jiyong Park2d492942018-03-05 17:44:10 +0900618 myLinkType := getLinkType(from)
619 otherLinkType := getLinkType(&to.Module)
620 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."
621
622 switch myLinkType {
623 case javaCore:
624 if otherLinkType != javaCore {
625 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 +0900626 ctx.OtherModuleName(to))
627 }
Jiyong Park2d492942018-03-05 17:44:10 +0900628 break
629 case javaSdk:
630 if otherLinkType != javaCore && otherLinkType != javaSdk {
631 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
632 ctx.OtherModuleName(to))
633 }
634 break
635 case javaSystem:
636 if otherLinkType == javaPlatform {
637 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
638 ctx.OtherModuleName(to))
639 }
640 break
641 case javaPlatform:
642 // no restriction on link-type
643 break
Jiyong Park750e5572018-01-31 00:20:13 +0900644 }
645}
646
Colin Cross32f676a2017-09-06 13:41:06 -0700647func (j *Module) collectDeps(ctx android.ModuleContext) deps {
648 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700649
Colin Cross300f0382018-03-06 13:11:51 -0800650 if ctx.Device() {
651 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
652 if sdkDep.invalidVersion {
653 ctx.AddMissingDependencies([]string{sdkDep.module})
654 } else if sdkDep.useFiles {
655 // sdkDep.jar is actually equivalent to turbine header.jar.
656 deps.classpath = append(deps.classpath, sdkDep.jar)
657 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
658 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700659 }
660
Colin Crossd11fcda2017-10-23 17:59:01 -0700661 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700662 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700663 tag := ctx.OtherModuleDependencyTag(module)
664
Jiyong Park750e5572018-01-31 00:20:13 +0900665 if to, ok := module.(*Library); ok {
666 checkLinkType(ctx, j, to, tag.(dependencyTag))
667 }
Colin Cross54250902017-12-05 09:28:08 -0800668 switch dep := module.(type) {
669 case Dependency:
670 switch tag {
671 case bootClasspathTag:
672 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
673 case libTag:
674 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
675 case staticLibTag:
676 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
677 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
678 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
679 case frameworkResTag:
680 if ctx.ModuleName() == "framework" {
681 // framework.jar has a one-off dependency on the R.java and Manifest.java files
682 // generated by framework-res.apk
683 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
684 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800685 case frameworkApkTag:
686 if ctx.ModuleName() == "android_stubs_current" ||
687 ctx.ModuleName() == "android_system_stubs_current" ||
688 ctx.ModuleName() == "android_test_stubs_current" {
689 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
690 // resource files out of there for aapt.
691 //
692 // Normally the package rule runs aapt, which includes the resource,
693 // but we're not running that in our package rule so just copy in the
694 // resource files here.
695 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
696 }
Colin Cross54250902017-12-05 09:28:08 -0800697 case kotlinStdlibTag:
698 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800699 }
700
701 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
702 case android.SourceFileProducer:
703 switch tag {
704 case libTag:
705 checkProducesJars(ctx, dep)
706 deps.classpath = append(deps.classpath, dep.Srcs()...)
707 case staticLibTag:
708 checkProducesJars(ctx, dep)
709 deps.classpath = append(deps.classpath, dep.Srcs()...)
710 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
711 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
712 case android.DefaultsDepTag, android.SourceDepTag:
713 // Nothing to do
714 default:
715 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
716 }
717 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700718 switch tag {
719 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700720 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700721 case systemModulesTag:
722 if deps.systemModules != nil {
723 panic("Found two system module dependencies")
724 }
725 sm := module.(*SystemModules)
726 if sm.outputFile == nil {
727 panic("Missing directory for system module dependency")
728 }
729 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700730 default:
731 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700732 }
Colin Crossec7a0422017-07-07 14:47:12 -0700733 }
Colin Cross2fe66872015-03-30 17:20:39 -0700734 })
735
Colin Cross32f676a2017-09-06 13:41:06 -0700736 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700737}
738
Nan Zhanged19fc32017-10-19 13:06:22 -0700739func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700740
Colin Crossf03c82b2015-04-13 13:53:40 -0700741 var flags javaBuilderFlags
742
Nan Zhanged19fc32017-10-19 13:06:22 -0700743 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700744 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800745 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700746 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700747 }
Colin Cross6510f912017-11-29 00:27:14 -0800748 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700749 // Override the -g flag passed globally to remove local variable debug info to reduce
750 // disk and memory usage.
751 javacFlags = append(javacFlags, "-g:source,lines")
752 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700753 if len(javacFlags) > 0 {
754 // optimization.
755 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
756 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700757 }
Colin Cross64162712017-08-08 13:17:59 -0700758
Andreas Gampef3e5b552018-01-22 21:27:21 -0800759 if len(j.properties.Errorprone.Javacflags) > 0 {
760 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
761 }
762
Nan Zhanged19fc32017-10-19 13:06:22 -0700763 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800764 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700765 if j.properties.Java_version != nil {
766 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700767 } else if ctx.Device() && sdk <= 23 {
768 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800769 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700770 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900771 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700772 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
773 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700774 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700775 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700776 }
777
Nan Zhanged19fc32017-10-19 13:06:22 -0700778 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800779 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
780 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800781
782 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
783 !Bool(j.properties.No_standard_libs) &&
784 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
785 // Give host-side tools a version of OpenJDK's standard libraries
786 // close to what they're targeting. As of Dec 2017, AOSP is only
787 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
788 //
789 // When building with OpenJDK 8, the following should have no
790 // effect since those jars would be available by default.
791 //
792 // When building with OpenJDK 9 but targeting a version < 1.8,
793 // putting them on the bootclasspath means that:
794 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
795 // b) references to existing APIs are not reinterpreted in an
796 // OpenJDK 9-specific way, eg. calls to subclasses of
797 // java.nio.Buffer as in http://b/70862583
798 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
799 flags.bootClasspath = append(flags.bootClasspath,
800 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
801 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800802 if Bool(j.properties.Use_tools_jar) {
803 flags.bootClasspath = append(flags.bootClasspath,
804 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
805 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800806 }
807
Nan Zhanged19fc32017-10-19 13:06:22 -0700808 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700809 if deps.systemModules != nil {
810 flags.systemModules = append(flags.systemModules, deps.systemModules)
811 }
812
Nan Zhanged19fc32017-10-19 13:06:22 -0700813 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700814 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700815 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700816 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700817 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
818 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700819 }
820
Nan Zhanged19fc32017-10-19 13:06:22 -0700821 return flags
822}
Colin Crossc0b06f12015-04-08 13:03:43 -0700823
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800824func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700825
Colin Crossebe1a512017-11-14 13:12:14 -0800826 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700827
828 deps := j.collectDeps(ctx)
829 flags := j.collectBuilderFlags(ctx, deps)
830
Colin Cross6510f912017-11-29 00:27:14 -0800831 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700832 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
833 }
834 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700835 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800836 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700837 }
838
Colin Crossaf050172017-11-15 23:01:59 -0800839 srcFiles = j.genSources(ctx, srcFiles, flags)
840
841 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700842 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800843 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700844
Colin Cross0a6e0072017-08-30 14:24:55 -0700845 var jars android.Paths
846
Colin Cross1ee23172017-10-18 14:44:18 -0700847 jarName := ctx.ModuleName() + ".jar"
848
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000849 javaSrcFiles := srcFiles.FilterByExt(".java")
850 var uniqueSrcFiles android.Paths
851 set := make(map[string]bool)
852 for _, v := range javaSrcFiles {
853 if _, found := set[v.String()]; !found {
854 set[v.String()] = true
855 uniqueSrcFiles = append(uniqueSrcFiles, v)
856 }
857 }
858
Colin Cross93e85952017-08-15 13:34:18 -0700859 if srcFiles.HasExt(".kt") {
860 // If there are kotlin files, compile them first but pass all the kotlin and java files
861 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
862 // won't emit any classes for them.
863
864 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000865
Colin Cross93e85952017-08-15 13:34:18 -0700866 if ctx.Device() {
867 flags.kotlincFlags += " -no-jdk"
868 }
869
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000870 var kotlinSrcFiles android.Paths
871 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
872 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
873
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000874 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700875 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
876 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
877
Colin Cross1ee23172017-10-18 14:44:18 -0700878 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000879 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700880 if ctx.Failed() {
881 return
882 }
883
884 // Make javac rule depend on the kotlinc rule
885 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000886
Colin Cross93e85952017-08-15 13:34:18 -0700887 // Jar kotlin classes into the final jar after javac
888 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000889
890 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
891 // (it's expected to be on device bootclasspath)
892 if !proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
893 jars = append(jars, deps.kotlinStdlib...)
894 }
Colin Cross93e85952017-08-15 13:34:18 -0700895 }
896
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800897 // Store the list of .java files that was passed to javac
898 j.compiledJavaSrcs = uniqueSrcFiles
899 j.compiledSrcJars = srcJars
900
Nan Zhang61eaedb2017-11-02 13:28:15 -0700901 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800902 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700903 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
904 enable_sharding = true
905 if len(j.properties.Annotation_processors) != 0 ||
906 len(j.properties.Annotation_processor_classes) != 0 {
907 ctx.PropertyErrorf("javac_shard_size",
908 "%q cannot be set when annotation processors are enabled.",
909 j.properties.Javac_shard_size)
910 }
911 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700912 // If sdk jar is java module, then directly return classesJar as header.jar
913 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
914 j.Name() != "android_test_stubs_current" {
915 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
916 if ctx.Failed() {
917 return
918 }
919 }
920 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700921 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700922 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800923 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700924 // If error-prone is enabled, add an additional rule to compile the java files into
925 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700926 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700927 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
928 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700929 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700930 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700931 extraJarDeps = append(extraJarDeps, errorprone)
932 }
933
Nan Zhang61eaedb2017-11-02 13:28:15 -0700934 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800935 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700936 shardSize := int(*(j.properties.Javac_shard_size))
937 var shardSrcs []android.Paths
938 if len(uniqueSrcFiles) > 0 {
939 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
940 for idx, shardSrc := range shardSrcs {
941 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
942 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
943 jars = append(jars, classes)
944 }
945 }
946 if len(srcJars) > 0 {
947 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
948 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
949 jars = append(jars, classes)
950 }
951 } else {
952 classes := android.PathForModuleOut(ctx, "javac", jarName)
953 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
954 jars = append(jars, classes)
955 }
Colin Crossd6891432017-09-27 17:39:56 -0700956 if ctx.Failed() {
957 return
958 }
Colin Cross2fe66872015-03-30 17:20:39 -0700959 }
960
Colin Cross0f37af02017-09-27 17:42:05 -0700961 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
962 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
963
964 var resArgs []string
965 var resDeps android.Paths
966
967 resArgs = append(resArgs, dirArgs...)
968 resDeps = append(resDeps, dirDeps...)
969
970 resArgs = append(resArgs, fileArgs...)
971 resDeps = append(resDeps, fileDeps...)
972
973 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700974 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700975 resArgs = append(resArgs, srcArgs...)
976 resDeps = append(resDeps, srcDeps...)
977 }
Colin Cross40a36712017-09-27 17:41:35 -0700978
979 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700980 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700981 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700982 if ctx.Failed() {
983 return
984 }
Colin Cross20978302015-04-10 17:05:07 -0700985
Colin Cross0a6e0072017-08-30 14:24:55 -0700986 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700987 }
988
Colin Cross6ade34f2017-09-15 13:00:47 -0700989 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700990 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800991 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700992
Colin Cross366938f2017-12-11 16:29:02 -0800993 var manifest android.OptionalPath
994 if j.properties.Manifest != nil {
995 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
996 }
Colin Cross635acc92017-09-12 22:50:46 -0700997
Colin Cross0a6e0072017-08-30 14:24:55 -0700998 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700999 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -07001000 var outputFile android.Path
1001
1002 if len(jars) == 1 && !manifest.Valid() {
1003 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -08001004 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1005 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1006 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -07001007 outputFile = jars[0]
1008 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001009 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001011 outputFile = combinedJar
1012 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001013
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001014 // Use renamed kotlin standard library?
1015 if srcFiles.HasExt(".kt") && proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
1016 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1017 TransformJarJar(ctx, jarjarFile, outputFile,
1018 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1019 outputFile = jarjarFile
1020 if ctx.Failed() {
1021 return
1022 }
1023 }
1024
Colin Cross0a6e0072017-08-30 14:24:55 -07001025 if j.properties.Jarjar_rules != nil {
1026 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001027 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001028 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001029 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1030 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -07001031 if ctx.Failed() {
1032 return
1033 }
1034 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001035 j.implementationJarFile = outputFile
1036 if j.headerJarFile == nil {
1037 j.headerJarFile = j.implementationJarFile
1038 }
Colin Cross2fe66872015-03-30 17:20:39 -07001039
Colin Cross6510f912017-11-29 00:27:14 -08001040 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001041 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1042 j.properties.Instrument = true
1043 }
1044 }
1045
Colin Cross3144dfc2018-01-03 15:06:47 -08001046 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001047 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1048 }
1049
1050 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001051 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001052 if ctx.Failed() {
1053 return
1054 }
Colin Cross2fe66872015-03-30 17:20:39 -07001055 }
Colin Crossb7a63242015-04-16 14:09:14 -07001056 ctx.CheckbuildFile(outputFile)
1057 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001058}
1059
Colin Cross8eadbf02017-10-24 17:46:00 -07001060func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001061 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1062
1063 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001064 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001065 // Compile java sources into turbine.jar.
1066 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1067 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1068 if ctx.Failed() {
1069 return nil
1070 }
1071 jars = append(jars, turbineJar)
1072 }
1073
1074 // Combine any static header libraries into classes-header.jar. If there is only
1075 // one input jar this step will be skipped.
1076 var headerJar android.Path
1077 jars = append(jars, deps.staticHeaderJars...)
1078
Colin Cross5c6ecc12017-10-23 18:12:27 -07001079 // we cannot skip the combine step for now if there is only one jar
1080 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1081 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1082 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1083 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001084
1085 if j.properties.Jarjar_rules != nil {
1086 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1087 // Transform classes.jar into classes-jarjar.jar
1088 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1089 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1090 headerJar = jarjarFile
1091 if ctx.Failed() {
1092 return nil
1093 }
1094 }
1095
1096 return headerJar
1097}
1098
Colin Crosscb933592017-11-22 13:49:43 -08001099func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1100 classesJar android.Path, jarName string) android.Path {
1101
Colin Cross7a3139e2017-12-19 13:57:50 -08001102 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001103
Colin Cross84c38822018-01-03 15:59:46 -08001104 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001105 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1106
1107 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1108
1109 j.jacocoReportClassesFile = jacocoReportClassesFile
1110
1111 return instrumentedJar
1112}
1113
Colin Crosscb933592017-11-22 13:49:43 -08001114// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1115// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1116func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1117 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001118 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001119 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001120 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001121 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001122 }
1123}
1124
Colin Cross59f1bb62017-09-27 17:59:10 -07001125func (j *Module) installable() bool {
1126 return j.properties.Installable == nil || *j.properties.Installable
1127}
1128
Colin Crossf506d872017-07-19 15:53:04 -07001129var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001130
Nan Zhanged19fc32017-10-19 13:06:22 -07001131func (j *Module) HeaderJars() android.Paths {
1132 return android.Paths{j.headerJarFile}
1133}
1134
1135func (j *Module) ImplementationJars() android.Paths {
1136 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001137}
1138
Colin Cross46c9b8b2017-06-22 16:51:17 -07001139func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001140 return j.exportAidlIncludeDirs
1141}
1142
Colin Cross46c9b8b2017-06-22 16:51:17 -07001143var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001144
Colin Cross46c9b8b2017-06-22 16:51:17 -07001145func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001146 return j.logtagsSrcs
1147}
1148
Colin Cross2fe66872015-03-30 17:20:39 -07001149//
1150// Java libraries (.jar file)
1151//
1152
Colin Crossf506d872017-07-19 15:53:04 -07001153type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001154 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001155}
1156
Colin Crossf506d872017-07-19 15:53:04 -07001157func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001158 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001159
Colin Cross59f1bb62017-09-27 17:59:10 -07001160 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001161 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1162 ctx.ModuleName()+".jar", j.outputFile)
1163 }
Colin Crossb7a63242015-04-16 14:09:14 -07001164}
1165
Colin Crossf506d872017-07-19 15:53:04 -07001166func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001167 j.deps(ctx)
1168}
1169
Colin Crossa60ead82017-10-02 18:10:21 -07001170func LibraryFactory(installable bool) func() android.Module {
1171 return func() android.Module {
1172 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001173
Colin Crossa60ead82017-10-02 18:10:21 -07001174 if !installable {
1175 module.properties.Installable = proptools.BoolPtr(false)
1176 }
Colin Cross2fe66872015-03-30 17:20:39 -07001177
Colin Crossa60ead82017-10-02 18:10:21 -07001178 module.AddProperties(
1179 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001180 &module.Module.deviceProperties,
1181 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001182
Colin Crossa60ead82017-10-02 18:10:21 -07001183 InitJavaModule(module, android.HostAndDeviceSupported)
1184 return module
1185 }
Colin Cross2fe66872015-03-30 17:20:39 -07001186}
1187
Colin Crossf506d872017-07-19 15:53:04 -07001188func LibraryHostFactory() android.Module {
1189 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001190
Colin Cross6af17aa2017-09-20 12:59:05 -07001191 module.AddProperties(
1192 &module.Module.properties,
1193 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001194
Colin Cross89536d42017-07-07 14:35:50 -07001195 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001196 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001197}
1198
1199//
1200// Java Binaries (.jar file plus wrapper script)
1201//
1202
Colin Crossf506d872017-07-19 15:53:04 -07001203type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001204 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001205 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001206}
1207
Colin Crossf506d872017-07-19 15:53:04 -07001208type Binary struct {
1209 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001210
Colin Crossf506d872017-07-19 15:53:04 -07001211 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001212
Colin Cross6b4a32d2017-12-05 13:42:45 -08001213 isWrapperVariant bool
1214
Colin Crossc3315992017-12-08 19:12:36 -08001215 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001216 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001217}
1218
Alex Light24237172017-10-26 09:46:21 -07001219func (j *Binary) HostToolPath() android.OptionalPath {
1220 return android.OptionalPathForPath(j.binaryFile)
1221}
1222
Colin Crossf506d872017-07-19 15:53:04 -07001223func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001224 if ctx.Arch().ArchType == android.Common {
1225 // Compile the jar
1226 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001227 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001228 // Handle the binary wrapper
1229 j.isWrapperVariant = true
1230
Colin Cross366938f2017-12-11 16:29:02 -08001231 if j.binaryProperties.Wrapper != nil {
1232 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001233 } else {
1234 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1235 }
1236
1237 // Depend on the installed jar so that the wrapper doesn't get executed by
1238 // another build rule before the jar has been installed.
1239 jarFile := ctx.PrimaryModule().(*Binary).installFile
1240
1241 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1242 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001243 }
Colin Cross2fe66872015-03-30 17:20:39 -07001244}
1245
Colin Crossf506d872017-07-19 15:53:04 -07001246func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001247 if ctx.Arch().ArchType == android.Common {
1248 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001249 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001250 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001251 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001252}
1253
Colin Crossf506d872017-07-19 15:53:04 -07001254func BinaryFactory() android.Module {
1255 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001256
Colin Cross36242852017-06-23 15:06:31 -07001257 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001258 &module.Module.properties,
1259 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001260 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001261 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001262
Colin Cross6b4a32d2017-12-05 13:42:45 -08001263 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1264 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001265 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001266}
1267
Colin Crossf506d872017-07-19 15:53:04 -07001268func BinaryHostFactory() android.Module {
1269 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001270
Colin Cross36242852017-06-23 15:06:31 -07001271 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001272 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001273 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001274 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001275
Colin Cross6b4a32d2017-12-05 13:42:45 -08001276 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1277 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001278 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001279}
1280
1281//
1282// Java prebuilts
1283//
1284
Colin Cross74d73e22017-08-02 11:05:49 -07001285type ImportProperties struct {
1286 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001287
Nan Zhangea568a42017-11-08 21:20:04 -08001288 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001289
1290 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001291}
1292
1293type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001294 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001295 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001296
Colin Cross74d73e22017-08-02 11:05:49 -07001297 properties ImportProperties
1298
Colin Cross0a6e0072017-08-30 14:24:55 -07001299 classpathFiles android.Paths
1300 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001301}
1302
Colin Cross74d73e22017-08-02 11:05:49 -07001303func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001304 return &j.prebuilt
1305}
1306
Colin Cross74d73e22017-08-02 11:05:49 -07001307func (j *Import) PrebuiltSrcs() []string {
1308 return j.properties.Jars
1309}
1310
1311func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001312 return j.prebuilt.Name(j.ModuleBase.Name())
1313}
1314
Colin Cross74d73e22017-08-02 11:05:49 -07001315func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001316}
1317
Colin Cross74d73e22017-08-02 11:05:49 -07001318func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1319 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001320
Colin Crosse9a275b2017-10-16 17:09:48 -07001321 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001322 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001323 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001324}
1325
Colin Cross74d73e22017-08-02 11:05:49 -07001326var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001327
Nan Zhanged19fc32017-10-19 13:06:22 -07001328func (j *Import) HeaderJars() android.Paths {
1329 return j.classpathFiles
1330}
1331
1332func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001333 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001334}
1335
Colin Cross74d73e22017-08-02 11:05:49 -07001336func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001337 return nil
1338}
1339
Colin Cross74d73e22017-08-02 11:05:49 -07001340var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001341
Colin Cross74d73e22017-08-02 11:05:49 -07001342func ImportFactory() android.Module {
1343 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001344
Colin Cross74d73e22017-08-02 11:05:49 -07001345 module.AddProperties(&module.properties)
1346
1347 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001348 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1349 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001350}
1351
Colin Cross74d73e22017-08-02 11:05:49 -07001352func ImportFactoryHost() android.Module {
1353 module := &Import{}
1354
1355 module.AddProperties(&module.properties)
1356
1357 android.InitPrebuiltModule(module, &module.properties.Jars)
1358 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1359 return module
1360}
1361
Colin Cross89536d42017-07-07 14:35:50 -07001362//
1363// Defaults
1364//
1365type Defaults struct {
1366 android.ModuleBase
1367 android.DefaultsModuleBase
1368}
1369
1370func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1371}
1372
1373func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1374}
1375
1376func defaultsFactory() android.Module {
1377 return DefaultsFactory()
1378}
1379
1380func DefaultsFactory(props ...interface{}) android.Module {
1381 module := &Defaults{}
1382
1383 module.AddProperties(props...)
1384 module.AddProperties(
1385 &CompilerProperties{},
1386 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001387 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001388 )
1389
1390 android.InitDefaultsModule(module)
1391
1392 return module
1393}
Nan Zhangea568a42017-11-08 21:20:04 -08001394
1395var Bool = proptools.Bool
1396var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001397var inList = android.InList