blob: 06dce052a249912268a020a1373f34987f5311f5 [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 Park750e5572018-01-31 00:20:13 +0900594func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
595 if strings.HasPrefix(String(from.deviceProperties.Sdk_version), "core_") {
596 if !strings.HasPrefix(String(to.deviceProperties.Sdk_version), "core_") {
597 ctx.ModuleErrorf("depends on other library %q using non-core Java APIs",
598 ctx.OtherModuleName(to))
599 }
600 }
601}
602
Colin Cross32f676a2017-09-06 13:41:06 -0700603func (j *Module) collectDeps(ctx android.ModuleContext) deps {
604 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700605
Colin Cross300f0382018-03-06 13:11:51 -0800606 if ctx.Device() {
607 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
608 if sdkDep.invalidVersion {
609 ctx.AddMissingDependencies([]string{sdkDep.module})
610 } else if sdkDep.useFiles {
611 // sdkDep.jar is actually equivalent to turbine header.jar.
612 deps.classpath = append(deps.classpath, sdkDep.jar)
613 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
614 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700615 }
616
Colin Crossd11fcda2017-10-23 17:59:01 -0700617 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700618 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700619 tag := ctx.OtherModuleDependencyTag(module)
620
Jiyong Park750e5572018-01-31 00:20:13 +0900621 if to, ok := module.(*Library); ok {
622 checkLinkType(ctx, j, to, tag.(dependencyTag))
623 }
Colin Cross54250902017-12-05 09:28:08 -0800624 switch dep := module.(type) {
625 case Dependency:
626 switch tag {
627 case bootClasspathTag:
628 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
629 case libTag:
630 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
631 case staticLibTag:
632 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
633 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
634 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
635 case frameworkResTag:
636 if ctx.ModuleName() == "framework" {
637 // framework.jar has a one-off dependency on the R.java and Manifest.java files
638 // generated by framework-res.apk
639 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
640 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800641 case frameworkApkTag:
642 if ctx.ModuleName() == "android_stubs_current" ||
643 ctx.ModuleName() == "android_system_stubs_current" ||
644 ctx.ModuleName() == "android_test_stubs_current" {
645 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
646 // resource files out of there for aapt.
647 //
648 // Normally the package rule runs aapt, which includes the resource,
649 // but we're not running that in our package rule so just copy in the
650 // resource files here.
651 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
652 }
Colin Cross54250902017-12-05 09:28:08 -0800653 case kotlinStdlibTag:
654 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800655 }
656
657 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
658 case android.SourceFileProducer:
659 switch tag {
660 case libTag:
661 checkProducesJars(ctx, dep)
662 deps.classpath = append(deps.classpath, dep.Srcs()...)
663 case staticLibTag:
664 checkProducesJars(ctx, dep)
665 deps.classpath = append(deps.classpath, dep.Srcs()...)
666 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
667 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
668 case android.DefaultsDepTag, android.SourceDepTag:
669 // Nothing to do
670 default:
671 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
672 }
673 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700674 switch tag {
675 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700676 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700677 case systemModulesTag:
678 if deps.systemModules != nil {
679 panic("Found two system module dependencies")
680 }
681 sm := module.(*SystemModules)
682 if sm.outputFile == nil {
683 panic("Missing directory for system module dependency")
684 }
685 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700686 default:
687 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700688 }
Colin Crossec7a0422017-07-07 14:47:12 -0700689 }
Colin Cross2fe66872015-03-30 17:20:39 -0700690 })
691
Colin Cross32f676a2017-09-06 13:41:06 -0700692 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700693}
694
Nan Zhanged19fc32017-10-19 13:06:22 -0700695func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700696
Colin Crossf03c82b2015-04-13 13:53:40 -0700697 var flags javaBuilderFlags
698
Nan Zhanged19fc32017-10-19 13:06:22 -0700699 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700700 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800701 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700702 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700703 }
Colin Cross6510f912017-11-29 00:27:14 -0800704 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700705 // Override the -g flag passed globally to remove local variable debug info to reduce
706 // disk and memory usage.
707 javacFlags = append(javacFlags, "-g:source,lines")
708 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700709 if len(javacFlags) > 0 {
710 // optimization.
711 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
712 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700713 }
Colin Cross64162712017-08-08 13:17:59 -0700714
Andreas Gampef3e5b552018-01-22 21:27:21 -0800715 if len(j.properties.Errorprone.Javacflags) > 0 {
716 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
717 }
718
Nan Zhanged19fc32017-10-19 13:06:22 -0700719 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800720 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700721 if j.properties.Java_version != nil {
722 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700723 } else if ctx.Device() && sdk <= 23 {
724 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800725 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700726 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900727 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700728 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
729 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700730 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700731 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700732 }
733
Nan Zhanged19fc32017-10-19 13:06:22 -0700734 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800735 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
736 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800737
738 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
739 !Bool(j.properties.No_standard_libs) &&
740 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
741 // Give host-side tools a version of OpenJDK's standard libraries
742 // close to what they're targeting. As of Dec 2017, AOSP is only
743 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
744 //
745 // When building with OpenJDK 8, the following should have no
746 // effect since those jars would be available by default.
747 //
748 // When building with OpenJDK 9 but targeting a version < 1.8,
749 // putting them on the bootclasspath means that:
750 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
751 // b) references to existing APIs are not reinterpreted in an
752 // OpenJDK 9-specific way, eg. calls to subclasses of
753 // java.nio.Buffer as in http://b/70862583
754 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
755 flags.bootClasspath = append(flags.bootClasspath,
756 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
757 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800758 if Bool(j.properties.Use_tools_jar) {
759 flags.bootClasspath = append(flags.bootClasspath,
760 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
761 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800762 }
763
Nan Zhanged19fc32017-10-19 13:06:22 -0700764 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700765 if deps.systemModules != nil {
766 flags.systemModules = append(flags.systemModules, deps.systemModules)
767 }
768
Nan Zhanged19fc32017-10-19 13:06:22 -0700769 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700770 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700771 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700772 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700773 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
774 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700775 }
776
Nan Zhanged19fc32017-10-19 13:06:22 -0700777 return flags
778}
Colin Crossc0b06f12015-04-08 13:03:43 -0700779
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800780func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700781
Colin Crossebe1a512017-11-14 13:12:14 -0800782 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700783
784 deps := j.collectDeps(ctx)
785 flags := j.collectBuilderFlags(ctx, deps)
786
Colin Cross6510f912017-11-29 00:27:14 -0800787 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700788 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
789 }
790 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700791 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800792 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700793 }
794
Colin Crossaf050172017-11-15 23:01:59 -0800795 srcFiles = j.genSources(ctx, srcFiles, flags)
796
797 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700798 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800799 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700800
Colin Cross0a6e0072017-08-30 14:24:55 -0700801 var jars android.Paths
802
Colin Cross1ee23172017-10-18 14:44:18 -0700803 jarName := ctx.ModuleName() + ".jar"
804
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000805 javaSrcFiles := srcFiles.FilterByExt(".java")
806 var uniqueSrcFiles android.Paths
807 set := make(map[string]bool)
808 for _, v := range javaSrcFiles {
809 if _, found := set[v.String()]; !found {
810 set[v.String()] = true
811 uniqueSrcFiles = append(uniqueSrcFiles, v)
812 }
813 }
814
Colin Cross93e85952017-08-15 13:34:18 -0700815 if srcFiles.HasExt(".kt") {
816 // If there are kotlin files, compile them first but pass all the kotlin and java files
817 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
818 // won't emit any classes for them.
819
820 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000821
Colin Cross93e85952017-08-15 13:34:18 -0700822 if ctx.Device() {
823 flags.kotlincFlags += " -no-jdk"
824 }
825
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000826 var kotlinSrcFiles android.Paths
827 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
828 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
829
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000830 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700831 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
832 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
833
Colin Cross1ee23172017-10-18 14:44:18 -0700834 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000835 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700836 if ctx.Failed() {
837 return
838 }
839
840 // Make javac rule depend on the kotlinc rule
841 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000842
Colin Cross93e85952017-08-15 13:34:18 -0700843 // Jar kotlin classes into the final jar after javac
844 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000845
846 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
847 // (it's expected to be on device bootclasspath)
848 if !proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
849 jars = append(jars, deps.kotlinStdlib...)
850 }
Colin Cross93e85952017-08-15 13:34:18 -0700851 }
852
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800853 // Store the list of .java files that was passed to javac
854 j.compiledJavaSrcs = uniqueSrcFiles
855 j.compiledSrcJars = srcJars
856
Nan Zhang61eaedb2017-11-02 13:28:15 -0700857 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800858 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700859 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
860 enable_sharding = true
861 if len(j.properties.Annotation_processors) != 0 ||
862 len(j.properties.Annotation_processor_classes) != 0 {
863 ctx.PropertyErrorf("javac_shard_size",
864 "%q cannot be set when annotation processors are enabled.",
865 j.properties.Javac_shard_size)
866 }
867 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700868 // If sdk jar is java module, then directly return classesJar as header.jar
869 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
870 j.Name() != "android_test_stubs_current" {
871 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
872 if ctx.Failed() {
873 return
874 }
875 }
876 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700877 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700878 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800879 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700880 // If error-prone is enabled, add an additional rule to compile the java files into
881 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700882 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700883 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
884 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700885 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700886 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700887 extraJarDeps = append(extraJarDeps, errorprone)
888 }
889
Nan Zhang61eaedb2017-11-02 13:28:15 -0700890 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800891 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700892 shardSize := int(*(j.properties.Javac_shard_size))
893 var shardSrcs []android.Paths
894 if len(uniqueSrcFiles) > 0 {
895 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
896 for idx, shardSrc := range shardSrcs {
897 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
898 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
899 jars = append(jars, classes)
900 }
901 }
902 if len(srcJars) > 0 {
903 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
904 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
905 jars = append(jars, classes)
906 }
907 } else {
908 classes := android.PathForModuleOut(ctx, "javac", jarName)
909 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
910 jars = append(jars, classes)
911 }
Colin Crossd6891432017-09-27 17:39:56 -0700912 if ctx.Failed() {
913 return
914 }
Colin Cross2fe66872015-03-30 17:20:39 -0700915 }
916
Colin Cross0f37af02017-09-27 17:42:05 -0700917 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
918 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
919
920 var resArgs []string
921 var resDeps android.Paths
922
923 resArgs = append(resArgs, dirArgs...)
924 resDeps = append(resDeps, dirDeps...)
925
926 resArgs = append(resArgs, fileArgs...)
927 resDeps = append(resDeps, fileDeps...)
928
929 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700930 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700931 resArgs = append(resArgs, srcArgs...)
932 resDeps = append(resDeps, srcDeps...)
933 }
Colin Cross40a36712017-09-27 17:41:35 -0700934
935 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700936 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700937 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700938 if ctx.Failed() {
939 return
940 }
Colin Cross20978302015-04-10 17:05:07 -0700941
Colin Cross0a6e0072017-08-30 14:24:55 -0700942 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700943 }
944
Colin Cross6ade34f2017-09-15 13:00:47 -0700945 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700946 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800947 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700948
Colin Cross366938f2017-12-11 16:29:02 -0800949 var manifest android.OptionalPath
950 if j.properties.Manifest != nil {
951 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
952 }
Colin Cross635acc92017-09-12 22:50:46 -0700953
Colin Cross0a6e0072017-08-30 14:24:55 -0700954 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700955 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700956 var outputFile android.Path
957
958 if len(jars) == 1 && !manifest.Valid() {
959 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -0800960 // TODO(ccross): this leaves any module-info.class files, but those should only come from
961 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
962 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -0700963 outputFile = jars[0]
964 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700965 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700966 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700967 outputFile = combinedJar
968 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700969
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000970 // Use renamed kotlin standard library?
971 if srcFiles.HasExt(".kt") && proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
972 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
973 TransformJarJar(ctx, jarjarFile, outputFile,
974 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
975 outputFile = jarjarFile
976 if ctx.Failed() {
977 return
978 }
979 }
980
Colin Cross0a6e0072017-08-30 14:24:55 -0700981 if j.properties.Jarjar_rules != nil {
982 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700983 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700984 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700985 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
986 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700987 if ctx.Failed() {
988 return
989 }
990 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700991 j.implementationJarFile = outputFile
992 if j.headerJarFile == nil {
993 j.headerJarFile = j.implementationJarFile
994 }
Colin Cross2fe66872015-03-30 17:20:39 -0700995
Colin Cross6510f912017-11-29 00:27:14 -0800996 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -0800997 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
998 j.properties.Instrument = true
999 }
1000 }
1001
Colin Cross3144dfc2018-01-03 15:06:47 -08001002 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001003 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1004 }
1005
1006 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001007 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001008 if ctx.Failed() {
1009 return
1010 }
Colin Cross2fe66872015-03-30 17:20:39 -07001011 }
Colin Crossb7a63242015-04-16 14:09:14 -07001012 ctx.CheckbuildFile(outputFile)
1013 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001014}
1015
Colin Cross8eadbf02017-10-24 17:46:00 -07001016func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001017 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1018
1019 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001020 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001021 // Compile java sources into turbine.jar.
1022 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1023 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1024 if ctx.Failed() {
1025 return nil
1026 }
1027 jars = append(jars, turbineJar)
1028 }
1029
1030 // Combine any static header libraries into classes-header.jar. If there is only
1031 // one input jar this step will be skipped.
1032 var headerJar android.Path
1033 jars = append(jars, deps.staticHeaderJars...)
1034
Colin Cross5c6ecc12017-10-23 18:12:27 -07001035 // we cannot skip the combine step for now if there is only one jar
1036 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1037 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1038 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1039 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001040
1041 if j.properties.Jarjar_rules != nil {
1042 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1043 // Transform classes.jar into classes-jarjar.jar
1044 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1045 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1046 headerJar = jarjarFile
1047 if ctx.Failed() {
1048 return nil
1049 }
1050 }
1051
1052 return headerJar
1053}
1054
Colin Crosscb933592017-11-22 13:49:43 -08001055func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1056 classesJar android.Path, jarName string) android.Path {
1057
Colin Cross7a3139e2017-12-19 13:57:50 -08001058 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001059
Colin Cross84c38822018-01-03 15:59:46 -08001060 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001061 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1062
1063 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1064
1065 j.jacocoReportClassesFile = jacocoReportClassesFile
1066
1067 return instrumentedJar
1068}
1069
Colin Crosscb933592017-11-22 13:49:43 -08001070// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1071// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1072func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1073 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001074 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001075 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001076 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001077 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001078 }
1079}
1080
Colin Cross59f1bb62017-09-27 17:59:10 -07001081func (j *Module) installable() bool {
1082 return j.properties.Installable == nil || *j.properties.Installable
1083}
1084
Colin Crossf506d872017-07-19 15:53:04 -07001085var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001086
Nan Zhanged19fc32017-10-19 13:06:22 -07001087func (j *Module) HeaderJars() android.Paths {
1088 return android.Paths{j.headerJarFile}
1089}
1090
1091func (j *Module) ImplementationJars() android.Paths {
1092 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001093}
1094
Colin Cross46c9b8b2017-06-22 16:51:17 -07001095func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001096 return j.exportAidlIncludeDirs
1097}
1098
Colin Cross46c9b8b2017-06-22 16:51:17 -07001099var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001100
Colin Cross46c9b8b2017-06-22 16:51:17 -07001101func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001102 return j.logtagsSrcs
1103}
1104
Colin Cross2fe66872015-03-30 17:20:39 -07001105//
1106// Java libraries (.jar file)
1107//
1108
Colin Crossf506d872017-07-19 15:53:04 -07001109type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001110 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001111}
1112
Colin Crossf506d872017-07-19 15:53:04 -07001113func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001114 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001115
Colin Cross59f1bb62017-09-27 17:59:10 -07001116 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001117 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1118 ctx.ModuleName()+".jar", j.outputFile)
1119 }
Colin Crossb7a63242015-04-16 14:09:14 -07001120}
1121
Colin Crossf506d872017-07-19 15:53:04 -07001122func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001123 j.deps(ctx)
1124}
1125
Colin Crossa60ead82017-10-02 18:10:21 -07001126func LibraryFactory(installable bool) func() android.Module {
1127 return func() android.Module {
1128 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001129
Colin Crossa60ead82017-10-02 18:10:21 -07001130 if !installable {
1131 module.properties.Installable = proptools.BoolPtr(false)
1132 }
Colin Cross2fe66872015-03-30 17:20:39 -07001133
Colin Crossa60ead82017-10-02 18:10:21 -07001134 module.AddProperties(
1135 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001136 &module.Module.deviceProperties,
1137 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001138
Colin Crossa60ead82017-10-02 18:10:21 -07001139 InitJavaModule(module, android.HostAndDeviceSupported)
1140 return module
1141 }
Colin Cross2fe66872015-03-30 17:20:39 -07001142}
1143
Colin Crossf506d872017-07-19 15:53:04 -07001144func LibraryHostFactory() android.Module {
1145 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001146
Colin Cross6af17aa2017-09-20 12:59:05 -07001147 module.AddProperties(
1148 &module.Module.properties,
1149 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001150
Colin Cross89536d42017-07-07 14:35:50 -07001151 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001152 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001153}
1154
1155//
1156// Java Binaries (.jar file plus wrapper script)
1157//
1158
Colin Crossf506d872017-07-19 15:53:04 -07001159type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001160 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001161 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001162}
1163
Colin Crossf506d872017-07-19 15:53:04 -07001164type Binary struct {
1165 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001166
Colin Crossf506d872017-07-19 15:53:04 -07001167 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001168
Colin Cross6b4a32d2017-12-05 13:42:45 -08001169 isWrapperVariant bool
1170
Colin Crossc3315992017-12-08 19:12:36 -08001171 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001172 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001173}
1174
Alex Light24237172017-10-26 09:46:21 -07001175func (j *Binary) HostToolPath() android.OptionalPath {
1176 return android.OptionalPathForPath(j.binaryFile)
1177}
1178
Colin Crossf506d872017-07-19 15:53:04 -07001179func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 if ctx.Arch().ArchType == android.Common {
1181 // Compile the jar
1182 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001183 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001184 // Handle the binary wrapper
1185 j.isWrapperVariant = true
1186
Colin Cross366938f2017-12-11 16:29:02 -08001187 if j.binaryProperties.Wrapper != nil {
1188 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001189 } else {
1190 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1191 }
1192
1193 // Depend on the installed jar so that the wrapper doesn't get executed by
1194 // another build rule before the jar has been installed.
1195 jarFile := ctx.PrimaryModule().(*Binary).installFile
1196
1197 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1198 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001199 }
Colin Cross2fe66872015-03-30 17:20:39 -07001200}
1201
Colin Crossf506d872017-07-19 15:53:04 -07001202func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001203 if ctx.Arch().ArchType == android.Common {
1204 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001205 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001206 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001207 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001208}
1209
Colin Crossf506d872017-07-19 15:53:04 -07001210func BinaryFactory() android.Module {
1211 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001212
Colin Cross36242852017-06-23 15:06:31 -07001213 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001214 &module.Module.properties,
1215 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001216 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001217 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001218
Colin Cross6b4a32d2017-12-05 13:42:45 -08001219 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1220 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001221 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001222}
1223
Colin Crossf506d872017-07-19 15:53:04 -07001224func BinaryHostFactory() android.Module {
1225 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001226
Colin Cross36242852017-06-23 15:06:31 -07001227 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001228 &module.Module.properties,
1229 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001230 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001231 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001232
Colin Cross6b4a32d2017-12-05 13:42:45 -08001233 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1234 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001235 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001236}
1237
1238//
1239// Java prebuilts
1240//
1241
Colin Cross74d73e22017-08-02 11:05:49 -07001242type ImportProperties struct {
1243 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001244
Nan Zhangea568a42017-11-08 21:20:04 -08001245 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001246
1247 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001248}
1249
1250type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001251 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001252 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001253
Colin Cross74d73e22017-08-02 11:05:49 -07001254 properties ImportProperties
1255
Colin Cross0a6e0072017-08-30 14:24:55 -07001256 classpathFiles android.Paths
1257 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001258}
1259
Colin Cross74d73e22017-08-02 11:05:49 -07001260func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001261 return &j.prebuilt
1262}
1263
Colin Cross74d73e22017-08-02 11:05:49 -07001264func (j *Import) PrebuiltSrcs() []string {
1265 return j.properties.Jars
1266}
1267
1268func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001269 return j.prebuilt.Name(j.ModuleBase.Name())
1270}
1271
Colin Cross74d73e22017-08-02 11:05:49 -07001272func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001273}
1274
Colin Cross74d73e22017-08-02 11:05:49 -07001275func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1276 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001277
Colin Crosse9a275b2017-10-16 17:09:48 -07001278 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001279 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001280 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001281}
1282
Colin Cross74d73e22017-08-02 11:05:49 -07001283var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001284
Nan Zhanged19fc32017-10-19 13:06:22 -07001285func (j *Import) HeaderJars() android.Paths {
1286 return j.classpathFiles
1287}
1288
1289func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001290 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001291}
1292
Colin Cross74d73e22017-08-02 11:05:49 -07001293func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001294 return nil
1295}
1296
Colin Cross74d73e22017-08-02 11:05:49 -07001297var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001298
Colin Cross74d73e22017-08-02 11:05:49 -07001299func ImportFactory() android.Module {
1300 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001301
Colin Cross74d73e22017-08-02 11:05:49 -07001302 module.AddProperties(&module.properties)
1303
1304 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001305 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1306 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001307}
1308
Colin Cross74d73e22017-08-02 11:05:49 -07001309func ImportFactoryHost() android.Module {
1310 module := &Import{}
1311
1312 module.AddProperties(&module.properties)
1313
1314 android.InitPrebuiltModule(module, &module.properties.Jars)
1315 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1316 return module
1317}
1318
Colin Cross89536d42017-07-07 14:35:50 -07001319//
1320// Defaults
1321//
1322type Defaults struct {
1323 android.ModuleBase
1324 android.DefaultsModuleBase
1325}
1326
1327func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1328}
1329
1330func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1331}
1332
1333func defaultsFactory() android.Module {
1334 return DefaultsFactory()
1335}
1336
1337func DefaultsFactory(props ...interface{}) android.Module {
1338 module := &Defaults{}
1339
1340 module.AddProperties(props...)
1341 module.AddProperties(
1342 &CompilerProperties{},
1343 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001344 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001345 )
1346
1347 android.InitDefaultsModule(module)
1348
1349 return module
1350}
Nan Zhangea568a42017-11-08 21:20:04 -08001351
1352var Bool = proptools.Bool
1353var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001354var inList = android.InList