diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 186 |
1 files changed, 128 insertions, 58 deletions
diff --git a/java/java.go b/java/java.go index 5e063a527..9cde82408 100644 --- a/java/java.go +++ b/java/java.go @@ -35,8 +35,8 @@ import ( func init() { android.RegisterModuleType("java_defaults", defaultsFactory) - android.RegisterModuleType("java_library", LibraryFactory) - android.RegisterModuleType("java_library_static", LibraryFactory) + android.RegisterModuleType("java_library", LibraryFactory(true)) + android.RegisterModuleType("java_library_static", LibraryFactory(false)) android.RegisterModuleType("java_library_host", LibraryHostFactory) android.RegisterModuleType("java_binary", BinaryFactory) android.RegisterModuleType("java_binary_host", BinaryHostFactory) @@ -49,7 +49,6 @@ func init() { // TODO: // Autogenerated files: -// Proto // Renderscript // Post-jar passes: // Proguard @@ -70,15 +69,25 @@ type CompilerProperties struct { Exclude_srcs []string `android:"arch_variant"` // list of directories containing Java resources - Resource_dirs []string `android:"arch_variant"` + Java_resource_dirs []string `android:"arch_variant"` - // list of directories that should be excluded from resource_dirs - Exclude_resource_dirs []string `android:"arch_variant"` + // list of directories that should be excluded from java_resource_dirs + Exclude_java_resource_dirs []string `android:"arch_variant"` - // don't build against the default libraries (legacy-test, core-junit, + // list of files to use as Java resources + Java_resources []string `android:"arch_variant"` + + // list of files that should be excluded from java_resources + Exclude_java_resources []string `android:"arch_variant"` + + // don't build against the default libraries (bootclasspath, legacy-test, core-junit, // ext, and framework for device targets) No_standard_libs *bool + // don't build against the framework libraries (legacy-test, core-junit, + // ext, and framework for device targets) + No_framework_libs *bool + // list of module-specific flags that will be used for javac compiles Javacflags []string `android:"arch_variant"` @@ -100,6 +109,9 @@ type CompilerProperties struct { // If set to false, don't allow this module to be installed. Defaults to true. Installable *bool + // If set to true, include sources used to compile the module in to the final jar + Include_srcs *bool + // List of modules to use as annotation processors Annotation_processors []string @@ -132,6 +144,7 @@ type Module struct { android.DefaultableModuleBase properties CompilerProperties + protoProperties android.ProtoProperties deviceProperties CompilerDeviceProperties // output file suitable for inserting into the classpath of another compile @@ -182,10 +195,11 @@ var ( ) type sdkDep struct { - useModule, useFiles, useDefaultLibs bool - module string - jar android.Path - aidl android.Path + useModule, useFiles, useDefaultLibs, invalidVersion bool + + module string + jar android.Path + aidl android.Path } func decodeSdkDep(ctx android.BaseContext, v string) sdkDep { @@ -205,14 +219,24 @@ func decodeSdkDep(ctx android.BaseContext, v string) sdkDep { aidl := filepath.Join(dir, "framework.aidl") jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar) aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl) + + if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() { + return sdkDep{ + invalidVersion: true, + module: "sdk_v" + v, + } + } + if !jarPath.Valid() { ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar) return sdkDep{} } + if !aidlPath.Valid() { ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl) return sdkDep{} } + return sdkDep{ useFiles: true, jar: jarPath.Path(), @@ -227,13 +251,7 @@ func decodeSdkDep(ctx android.BaseContext, v string) sdkDep { } } - if ctx.AConfig().UnbundledBuild() { - if v == "" { - if ctx, ok := ctx.(android.ModuleContext); ok { - ctx.AddMissingDependencies([]string{"sdk_version_must_be_set_for_modules_used_in_unbundled_builds"}) - } - return sdkDep{} - } + if ctx.AConfig().UnbundledBuild() && v != "" { return toFile(v) } @@ -259,15 +277,15 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) { sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version) if sdkDep.useDefaultLibs { ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...) - ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...) + if !proptools.Bool(j.properties.No_framework_libs) { + ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...) + } } if sdkDep.useModule { ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module) } } else { - if j.deviceProperties.Dex { - ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...) - } + // TODO(ccross): add hostdex support } } ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...) @@ -275,6 +293,25 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) { ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...) android.ExtractSourcesDeps(ctx, j.properties.Srcs) + android.ExtractSourcesDeps(ctx, j.properties.Java_resources) + + if j.hasSrcExt(".proto") { + protoDeps(ctx, &j.protoProperties) + } +} + +func hasSrcExt(srcs []string, ext string) bool { + for _, src := range srcs { + if filepath.Ext(src) == ext { + return true + } + } + + return false +} + +func (j *Module) hasSrcExt(ext string) bool { + return hasSrcExt(j.properties.Srcs, ext) } func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath, @@ -313,7 +350,9 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { var deps deps sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version) - if sdkDep.useFiles { + if sdkDep.invalidVersion { + ctx.AddMissingDependencies([]string{sdkDep.module}) + } else if sdkDep.useFiles { deps.classpath = append(deps.classpath, sdkDep.jar) deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl) } @@ -377,8 +416,6 @@ func (j *Module) compile(ctx android.ModuleContext) { flags.javaVersion = "${config.DefaultJavaVersion}" } - var extraDeps android.Paths - flags.bootClasspath.AddPaths(deps.bootClasspath) flags.classpath.AddPaths(deps.classpath) @@ -395,7 +432,15 @@ func (j *Module) compile(ctx android.ModuleContext) { srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs) - srcFiles = j.genSources(ctx, srcFiles, flags) + if hasSrcExt(srcFiles.Strings(), ".proto") { + flags = protoFlags(ctx, &j.protoProperties, flags) + } + + var srcFileLists android.Paths + + srcFiles, srcFileLists = j.genSources(ctx, srcFiles, flags) + + srcFileLists = append(srcFileLists, deps.srcFileLists...) ctx.VisitDirectDeps(func(module blueprint.Module) { if gen, ok := module.(genrule.SourceFileGenerator); ok { @@ -403,37 +448,52 @@ func (j *Module) compile(ctx android.ModuleContext) { } }) - deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...) - - var extraJarDeps android.Paths + srcFileLists = append(srcFileLists, j.ExtraSrcLists...) var jars android.Paths if len(srcFiles) > 0 { - // Compile java sources into .class files - classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraDeps) - if ctx.Failed() { - return - } - + var extraJarDeps android.Paths if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") { // If error-prone is enabled, add an additional rule to compile the java files into // a separate set of classes (so that they don't overwrite the normal ones and require - // a rebuild when error-prone is turned off). Add the classes as a dependency to - // the jar command so the two compiles can run in parallel. + // a rebuild when error-prone is turned off). // TODO(ccross): Once we always compile with javac9 we may be able to conditionally // enable error-prone without affecting the output class files. - errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, extraDeps) + errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags) extraJarDeps = append(extraJarDeps, errorprone) } + // Compile java sources into .class files + classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, extraJarDeps) + if ctx.Failed() { + return + } + jars = append(jars, classes) } - resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs) - if len(resourceJarSpecs) > 0 { + dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs) + fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources) + + var resArgs []string + var resDeps android.Paths + + resArgs = append(resArgs, dirArgs...) + resDeps = append(resDeps, dirDeps...) + + resArgs = append(resArgs, fileArgs...) + resDeps = append(resDeps, fileDeps...) + + if proptools.Bool(j.properties.Include_srcs) { + srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs) + resArgs = append(resArgs, srcArgs...) + resDeps = append(resDeps, srcDeps...) + } + + if len(resArgs) > 0 { // Combine classes + resources into classes-full-debug.jar - resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, extraJarDeps) + resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps) if ctx.Failed() { return } @@ -452,12 +512,12 @@ func (j *Module) compile(ctx android.ModuleContext) { manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest) // Combine the classes built from sources, any manifests, and any static libraries into - // classes-combined.jar. If there is only one input jar this step will be skipped. + // classes.jar. If there is only one input jar this step will be skipped. outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false) if j.properties.Jarjar_rules != nil { jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules) - // Transform classes-combined.jar into classes-jarjar.jar + // Transform classes.jar into classes-jarjar.jar outputFile = TransformJarJar(ctx, outputFile, jarjar_rules) if ctx.Failed() { return @@ -467,7 +527,7 @@ func (j *Module) compile(ctx android.ModuleContext) { j.classpathFile = outputFile // TODO(ccross): handle hostdex - if ctx.Device() && len(srcFiles) > 0 { + if ctx.Device() && j.installable() { dxFlags := j.deviceProperties.Dxflags if false /* emma enabled */ { // If you instrument class files that have local variable debug information in @@ -520,10 +580,6 @@ func (j *Module) compile(ctx android.ModuleContext) { return } - // TODO(ccross): For now, use the desugared jar as the classpath file. Eventually this - // might cause problems because desugar wants non-desugared jars in its class path. - j.classpathFile = desugarJar - // Compile classes.jar into classes.dex dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags) if ctx.Failed() { @@ -541,6 +597,10 @@ func (j *Module) compile(ctx android.ModuleContext) { j.outputFile = outputFile } +func (j *Module) installable() bool { + return j.properties.Installable == nil || *j.properties.Installable +} + var _ Dependency = (*Library)(nil) func (j *Module) ClasspathFiles() android.Paths { @@ -572,7 +632,7 @@ type Library struct { func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.compile(ctx) - if j.properties.Installable == nil || *j.properties.Installable == true { + if j.installable() { j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile) } @@ -582,23 +642,31 @@ func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) { j.deps(ctx) } -func LibraryFactory() android.Module { - module := &Library{} +func LibraryFactory(installable bool) func() android.Module { + return func() android.Module { + module := &Library{} - module.deviceProperties.Dex = true + if !installable { + module.properties.Installable = proptools.BoolPtr(false) + } + module.deviceProperties.Dex = true - module.AddProperties( - &module.Module.properties, - &module.Module.deviceProperties) + module.AddProperties( + &module.Module.properties, + &module.Module.deviceProperties, + &module.Module.protoProperties) - InitJavaModule(module, android.HostAndDeviceSupported) - return module + InitJavaModule(module, android.HostAndDeviceSupported) + return module + } } func LibraryHostFactory() android.Module { module := &Library{} - module.AddProperties(&module.Module.properties) + module.AddProperties( + &module.Module.properties, + &module.Module.protoProperties) InitJavaModule(module, android.HostSupported) return module @@ -644,6 +712,7 @@ func BinaryFactory() android.Module { module.AddProperties( &module.Module.properties, &module.Module.deviceProperties, + &module.Module.protoProperties, &module.binaryProperties) InitJavaModule(module, android.HostAndDeviceSupported) @@ -656,6 +725,7 @@ func BinaryHostFactory() android.Module { module.AddProperties( &module.Module.properties, &module.Module.deviceProperties, + &module.Module.protoProperties, &module.binaryProperties) InitJavaModule(module, android.HostSupported) |