From 702e0f8ab03edf50e55d08f8b78595462f1f564f Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 18 Oct 2017 17:27:54 -0700 Subject: [PATCH 1/2] Use full relative paths to get module outputs in tests Make Rel() on ModuleOutPath and ModuleGenPath return the path relative to the module out and module gen directories respectively, and make TestingModule.Output() match against the full relative path to the module. Test: java_test.go still passes Change-Id: Id5b2ec3fdef41d4169b943e68d032fc64a2b6f92 --- android/paths.go | 24 ++++++++++++++++++------ android/testing.go | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/android/paths.go b/android/paths.go index ed1e607f8..744354739 100644 --- a/android/paths.go +++ b/android/paths.go @@ -199,9 +199,9 @@ func PathsForSource(ctx PathContext, paths []string) Paths { if pathConfig(ctx).AllowMissingDependencies() { if modCtx, ok := ctx.(ModuleContext); ok { ret := make(Paths, 0, len(paths)) - intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing") + intermediates := pathForModule(modCtx).withRel("missing") for _, path := range paths { - p := ExistentPathForSource(ctx, intermediates, path) + p := ExistentPathForSource(ctx, intermediates.String(), path) if p.Valid() { ret = append(ret, p.Path()) } else { @@ -572,6 +572,12 @@ type OutputPath struct { basePath } +func (p OutputPath) withRel(rel string) OutputPath { + p.basePath.path = filepath.Join(p.basePath.path, rel) + p.basePath.rel = rel + return p +} + var _ Path = OutputPath{} // PathForOutput joins the provided paths and returns an OutputPath that is @@ -666,6 +672,10 @@ type ModuleOutPath struct { var _ Path = ModuleOutPath{} +func pathForModule(ctx ModuleContext) OutputPath { + return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) +} + // PathForVndkRefDump returns an OptionalPath representing the path of the reference // abi dump for the given module. This is not guaranteed to be valid. func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNdk, isSourceDump bool) OptionalPath { @@ -694,14 +704,15 @@ func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNd // output directory. func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath { p := validatePath(ctx, paths...) - return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)} + return ModuleOutPath{ + OutputPath: pathForModule(ctx).withRel(p), + } } // ModuleGenPath is a Path representing the 'gen' directory in a module's output // directory. Mainly used for generated sources. type ModuleGenPath struct { ModuleOutPath - path string } var _ Path = ModuleGenPath{} @@ -713,8 +724,9 @@ var _ objPathProvider = ModuleGenPath{} func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath { p := validatePath(ctx, paths...) return ModuleGenPath{ - PathForModuleOut(ctx, "gen", p), - p, + ModuleOutPath: ModuleOutPath{ + OutputPath: pathForModule(ctx).withRel("gen").withRel(p), + }, } } diff --git a/android/testing.go b/android/testing.go index 667c1aa8d..62b91e2e4 100644 --- a/android/testing.go +++ b/android/testing.go @@ -102,7 +102,7 @@ func (m TestingModule) Output(file string) ModuleBuildParams { outputs = append(outputs, p.Output) } for _, f := range outputs { - if f.Base() == file { + if f.Rel() == file { return p } } From 1ee2317555954d49086fa58d46690ef933eb1ae9 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 18 Oct 2017 14:44:18 -0700 Subject: [PATCH 2/2] Move all jar intermediates to subdirs Make all the jar intermediates called modulename.jar, and put each in a subdir for the tool that generated it. This will simplify using the jars as inputs to genrules and resources. Test: java_test.go Change-Id: If00e16bd7df5a4ba24ebc4b68c9ccf2cfda49544 --- java/builder.go | 14 +++++++------- java/java.go | 24 +++++++++++++----------- java/java_test.go | 20 ++++++++++---------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/java/builder.go b/java/builder.go index 9086d5185..4aaf84127 100644 --- a/java/builder.go +++ b/java/builder.go @@ -160,7 +160,7 @@ func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.Writ srcFiles android.Paths, srcJars classpath, flags javaBuilderFlags) { - classDir := android.PathForModuleOut(ctx, "classes-kt") + classDir := android.PathForModuleOut(ctx, "kotlinc", "classes") inputs := append(android.Paths(nil), srcFiles...) inputs = append(inputs, srcJars...) @@ -184,7 +184,7 @@ func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.Writab flags javaBuilderFlags, deps android.Paths) { transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, deps, - "", "javac", javac) + "javac", "javac", javac) } func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath, @@ -196,7 +196,7 @@ func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath, } transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, nil, - "-errorprone", "errorprone", errorprone) + "errorprone", "errorprone", errorprone) } // transformJavaToClasses takes source files and converts them to a jar containing .class files. @@ -211,7 +211,7 @@ func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath, func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, srcFiles android.Paths, srcJars classpath, flags javaBuilderFlags, deps android.Paths, - intermediatesSuffix, desc string, rule blueprint.Rule) { + intermediatesDir, desc string, rule blueprint.Rule) { deps = append(deps, srcJars...) @@ -237,8 +237,8 @@ func transformJavaToClasses(ctx android.ModuleContext, outputFile android.Writab "bootClasspath": bootClasspath, "sourcepath": srcJars.JavaSourcepath(), "classpath": flags.classpath.JavaClasspath(), - "outDir": android.PathForModuleOut(ctx, "classes"+intermediatesSuffix).String(), - "annoDir": android.PathForModuleOut(ctx, "anno"+intermediatesSuffix).String(), + "outDir": android.PathForModuleOut(ctx, intermediatesDir, "classes").String(), + "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(), "javaVersion": flags.javaVersion, }, }) @@ -288,7 +288,7 @@ func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePa func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath, classesJar android.Path, flags javaBuilderFlags) { - dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes") + dumpDir := android.PathForModuleOut(ctx, "desugar", "classes") javaFlags := "" if ctx.AConfig().UseOpenJDK9() { diff --git a/java/java.go b/java/java.go index 76e574eba..5310bc800 100644 --- a/java/java.go +++ b/java/java.go @@ -502,6 +502,8 @@ func (j *Module) compile(ctx android.ModuleContext) { var jars android.Paths + jarName := ctx.ModuleName() + ".jar" + if srcFiles.HasExt(".kt") { // If there are kotlin files, compile them first but pass all the kotlin and java files // kotlinc will use the java files to resolve types referenced by the kotlin files, but @@ -515,7 +517,7 @@ func (j *Module) compile(ctx android.ModuleContext) { flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...) flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...) - kotlinJar := android.PathForModuleOut(ctx, "classes-kt.jar") + kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName) TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags) if ctx.Failed() { return @@ -536,13 +538,13 @@ func (j *Module) compile(ctx android.ModuleContext) { // 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 := android.PathForModuleOut(ctx, "classes-errorprone.list") + errorprone := android.PathForModuleOut(ctx, "errorprone", jarName) RunErrorProne(ctx, errorprone, javaSrcFiles, srcJars, flags) extraJarDeps = append(extraJarDeps, errorprone) } // Compile java sources into .class files - classes := android.PathForModuleOut(ctx, "classes-compiled.jar") + classes := android.PathForModuleOut(ctx, "javac", jarName) TransformJavaToClasses(ctx, classes, javaSrcFiles, srcJars, flags, extraJarDeps) if ctx.Failed() { return @@ -570,7 +572,7 @@ func (j *Module) compile(ctx android.ModuleContext) { } if len(resArgs) > 0 { - resourceJar := android.PathForModuleOut(ctx, "res.jar") + resourceJar := android.PathForModuleOut(ctx, "res", jarName) TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps) if ctx.Failed() { return @@ -592,7 +594,7 @@ func (j *Module) compile(ctx android.ModuleContext) { // Optimization: skip the combine step if there is nothing to do outputFile = jars[0] } else { - combinedJar := android.PathForModuleOut(ctx, "classes.jar") + combinedJar := android.PathForModuleOut(ctx, "combined", jarName) TransformJarsToJar(ctx, combinedJar, jars, manifest, false) outputFile = combinedJar } @@ -600,7 +602,7 @@ func (j *Module) compile(ctx android.ModuleContext) { if j.properties.Jarjar_rules != nil { jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules) // Transform classes.jar into classes-jarjar.jar - jarjarFile := android.PathForModuleOut(ctx, "classes-jarjar.jar") + jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName) TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules) outputFile = jarjarFile if ctx.Failed() { @@ -658,17 +660,17 @@ func (j *Module) compile(ctx android.ModuleContext) { flags.desugarFlags = strings.Join(desugarFlags, " ") - desugarJar := android.PathForModuleOut(ctx, "classes-desugar.jar") + desugarJar := android.PathForModuleOut(ctx, "desugar", jarName) TransformDesugar(ctx, desugarJar, outputFile, flags) outputFile = desugarJar if ctx.Failed() { return } - // Compile classes.jar into classes.dex and then javalib.jar - javalibJar := android.PathForModuleOut(ctx, "javalib.jar") - TransformClassesJarToDexJar(ctx, javalibJar, desugarJar, flags) - outputFile = javalibJar + // Compile classes.jar into classes.dex and then a dex jar + dexJar := android.PathForModuleOut(ctx, "dex", jarName) + TransformClassesJarToDexJar(ctx, dexJar, desugarJar, flags) + outputFile = dexJar if ctx.Failed() { return } diff --git a/java/java_test.go b/java/java_test.go index d64688f6f..6dde93862 100644 --- a/java/java_test.go +++ b/java/java_test.go @@ -142,7 +142,7 @@ func moduleToPath(name string) string { case strings.HasSuffix(name, ".jar"): return name default: - return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar") + return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar") } } @@ -173,8 +173,8 @@ func TestSimple(t *testing.T) { t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) } - bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") - baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") + bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String() + baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() if !strings.Contains(javac.Args["classpath"], bar) { t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) @@ -465,12 +465,12 @@ func TestDefaults(t *testing.T) { t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) } - bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") + bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String() if !strings.Contains(javac.Args["classpath"], bar) { t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) } - baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") + baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) } @@ -530,8 +530,8 @@ func TestResources(t *testing.T) { } `+test.extra) - foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar") - fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") + foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") + fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { t.Errorf("foo combined jars %v does not contain %q", @@ -563,7 +563,7 @@ func TestExcludeResources(t *testing.T) { } `) - fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") + fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") expected := "-C res -f res/a -f res/b" if fooRes.Args["jarArgs"] != expected { @@ -572,7 +572,7 @@ func TestExcludeResources(t *testing.T) { } - barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar") + barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") expected = "-C . -f res/a" if barRes.Args["jarArgs"] != expected { @@ -625,7 +625,7 @@ func TestKotlin(t *testing.T) { kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") - jar := ctx.ModuleForTests("foo", "android_common").Output("classes.jar") + jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {