Make javac rules output a jar with soong_zip

Make javac rules output a jar file instead of a classes.list.
Combine the output jar, static jar dependencies, and resources
into the final jar using a separate rule.  For now, use a shell
command with unzip and soong_zip to create the final jar,
eventually it will be done with a zip2zip-style jar combiner.

Bug: 64691570
Test: java_test.go
Change-Id: Id8e6313e0097b78947d88e86e47b56ad08caca1a
This commit is contained in:
Colin Cross 2017-08-30 14:24:55 -07:00
parent fee57cb33e
commit 0a6e007e06
5 changed files with 112 additions and 176 deletions

View File

@ -43,8 +43,8 @@ var (
`$javacFlags $bootClasspath $classpath ` +
`-source $javaVersion -target $javaVersion ` +
`-d $outDir -s $annoDir @$out.rsp && ` +
`find $outDir -type f | sort | ${config.JarArgsCmd} $outDir > $out`,
CommandDeps: []string{"${config.JavacCmd}", "${config.JarArgsCmd}"},
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"},
Rspfile: "$out.rsp",
RspfileContent: "$in",
},
@ -57,12 +57,12 @@ var (
`$javacFlags $bootClasspath $classpath ` +
`-source $javaVersion -target $javaVersion ` +
`-d $outDir -s $annoDir @$out.rsp && ` +
`find $outDir -type f | sort | ${config.JarArgsCmd} $outDir > $out`,
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
CommandDeps: []string{
"${config.JavaCmd}",
"${config.ErrorProneJavacJar}",
"${config.ErrorProneJar}",
"${config.JarArgsCmd}",
"${config.SoongZipCmd}",
},
Rspfile: "$out.rsp",
RspfileContent: "$in",
@ -71,17 +71,24 @@ var (
jar = pctx.AndroidStaticRule("jar",
blueprint.RuleParams{
Command: `${config.JarCmd} $operation ${out}.tmp $manifest $jarArgs && ${config.Zip2ZipCmd} -t -i ${out}.tmp -o ${out} && rm ${out}.tmp`,
CommandDeps: []string{"${config.JarCmd}"},
Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
CommandDeps: []string{"${config.SoongZipCmd}"},
},
"operation", "manifest", "jarArgs")
"jarArgs")
combineJar = pctx.AndroidStaticRule("combineJar",
blueprint.RuleParams{
Command: `${config.MergeZipsCmd} -j $out $in`,
CommandDeps: []string{"${config.MergeZipsCmd}"},
},
"outDir")
dx = pctx.AndroidStaticRule("dx",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
`find "$outDir" -name "classes*.dex" | sort | ${config.JarArgsCmd} ${outDir} > $out`,
CommandDeps: []string{"${config.DxCmd}", "${config.JarArgsCmd}"},
`${config.DxCmd} --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
`find "$outDir" -name "classes*.dex" | sort > $out`,
CommandDeps: []string{"${config.DxCmd}"},
},
"outDir", "dxFlags")
@ -91,22 +98,6 @@ var (
CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
},
"rulesFile")
extractPrebuilt = pctx.AndroidStaticRule("extractPrebuilt",
blueprint.RuleParams{
Command: `rm -rf $outDir && unzip -qo $in -d $outDir && ` +
`find $outDir -name "*.class" | sort | ${config.JarArgsCmd} ${outDir} > $classFile && ` +
`find $outDir -type f -a \! -name "*.class" -a \! -name "MANIFEST.MF" | sort | ${config.JarArgsCmd} ${outDir} > $resourceFile`,
CommandDeps: []string{"${config.JarArgsCmd}"},
},
"outDir", "classFile", "resourceFile")
fileListToJarArgs = pctx.AndroidStaticRule("fileListToJarArgs",
blueprint.RuleParams{
Command: `${config.JarArgsCmd} -f $in -p ${outDir} -o $out`,
CommandDeps: []string{"${config.JarjarCmd}"},
},
"outDir")
)
func init() {
@ -123,23 +114,19 @@ type javaBuilderFlags struct {
}
type jarSpec struct {
android.ModuleOutPath
fileList, dir android.Path
}
func (j jarSpec) jarArgs() string {
return "@" + j.String()
func (j jarSpec) soongJarArgs() string {
return "-C " + j.dir.String() + " -l " + j.fileList.String()
}
func (j jarSpec) path() android.Path {
return j.ModuleOutPath
}
func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, srcFileLists android.Paths,
flags javaBuilderFlags, deps android.Paths) jarSpec {
func TransformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
flags javaBuilderFlags, deps android.Paths) android.ModuleOutPath {
classDir := android.PathForModuleOut(ctx, "classes")
annoDir := android.PathForModuleOut(ctx, "anno")
classFileList := android.PathForModuleOut(ctx, "classes.list")
classJar := android.PathForModuleOut(ctx, "classes.jar")
javacFlags := flags.javacFlags + android.JoinWithPrefix(srcFileLists.Strings(), "@")
@ -148,7 +135,7 @@ func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, s
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: javac,
Description: "javac",
Output: classFileList,
Output: classJar,
Inputs: srcFiles,
Implicits: deps,
Args: map[string]string{
@ -161,7 +148,7 @@ func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, s
},
})
return jarSpec{classFileList}
return classJar
}
func RunErrorProne(ctx android.ModuleContext, srcFiles android.Paths, srcFileLists android.Paths,
@ -199,22 +186,21 @@ func RunErrorProne(ctx android.ModuleContext, srcFiles android.Paths, srcFileLis
return classFileList
}
func TransformClassesToJar(ctx android.ModuleContext, classes []jarSpec,
func TransformResourcesToJar(ctx android.ModuleContext, resources []jarSpec,
manifest android.OptionalPath, deps android.Paths) android.Path {
outputFile := android.PathForModuleOut(ctx, "classes-full-debug.jar")
outputFile := android.PathForModuleOut(ctx, "res.jar")
jarArgs := []string{}
for _, j := range classes {
deps = append(deps, j.path())
jarArgs = append(jarArgs, j.jarArgs())
for _, j := range resources {
deps = append(deps, j.fileList)
jarArgs = append(jarArgs, j.soongJarArgs())
}
operation := "cf"
if manifest.Valid() {
operation = "cfm"
deps = append(deps, manifest.Path())
jarArgs = append(jarArgs, "-m "+manifest.String())
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
@ -224,14 +210,30 @@ func TransformClassesToJar(ctx android.ModuleContext, classes []jarSpec,
Implicits: deps,
Args: map[string]string{
"jarArgs": strings.Join(jarArgs, " "),
"operation": operation,
"manifest": manifest.String(),
},
})
return outputFile
}
func TransformJarsToJar(ctx android.ModuleContext, stem string, jars android.Paths) android.Path {
outputFile := android.PathForModuleOut(ctx, stem)
if len(jars) == 1 {
return jars[0]
}
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: combineJar,
Description: "combine jars",
Output: outputFile,
Inputs: jars,
})
return outputFile
}
func TransformClassesJarToDex(ctx android.ModuleContext, classesJar android.Path,
flags javaBuilderFlags) jarSpec {
@ -249,7 +251,7 @@ func TransformClassesJarToDex(ctx android.ModuleContext, classesJar android.Path
},
})
return jarSpec{outputFile}
return jarSpec{outputFile, outDir}
}
func TransformDexToJavaLib(ctx android.ModuleContext, resources []jarSpec,
@ -260,12 +262,12 @@ func TransformDexToJavaLib(ctx android.ModuleContext, resources []jarSpec,
var jarArgs []string
for _, j := range resources {
deps = append(deps, j.path())
jarArgs = append(jarArgs, j.jarArgs())
deps = append(deps, j.fileList)
jarArgs = append(jarArgs, j.soongJarArgs())
}
deps = append(deps, dexJarSpec.path())
jarArgs = append(jarArgs, dexJarSpec.jarArgs())
deps = append(deps, dexJarSpec.fileList)
jarArgs = append(jarArgs, dexJarSpec.soongJarArgs())
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: jar,
@ -273,7 +275,6 @@ func TransformDexToJavaLib(ctx android.ModuleContext, resources []jarSpec,
Output: outputFile,
Implicits: deps,
Args: map[string]string{
"operation": "cf",
"jarArgs": strings.Join(jarArgs, " "),
},
})
@ -281,7 +282,7 @@ func TransformDexToJavaLib(ctx android.ModuleContext, resources []jarSpec,
return outputFile
}
func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.Path {
func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.ModuleOutPath {
outputFile := android.PathForModuleOut(ctx, "classes-jarjar.jar")
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: jarjar,
@ -296,41 +297,3 @@ func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFi
return outputFile
}
func TransformPrebuiltJarToClasses(ctx android.ModuleContext,
subdir string, prebuilt android.Path) (classJarSpec, resourceJarSpec jarSpec) {
classDir := android.PathForModuleOut(ctx, subdir, "classes")
classFileList := android.PathForModuleOut(ctx, subdir, "classes.list")
resourceFileList := android.PathForModuleOut(ctx, subdir, "resources.list")
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: extractPrebuilt,
Description: "extract classes",
Outputs: android.WritablePaths{classFileList, resourceFileList},
Input: prebuilt,
Args: map[string]string{
"outDir": classDir.String(),
"classFile": classFileList.String(),
"resourceFile": resourceFileList.String(),
},
})
return jarSpec{classFileList}, jarSpec{resourceFileList}
}
func TransformFileListToJarSpec(ctx android.ModuleContext, dir, fileListFile android.Path) jarSpec {
outputFile := android.PathForModuleOut(ctx, fileListFile.Base()+".jarArgs")
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: fileListToJarArgs,
Description: "file list to jar args",
Output: outputFile,
Input: fileListFile,
Args: map[string]string{
"outDir": dir.String(),
},
})
return jarSpec{outputFile}
}

View File

@ -57,8 +57,9 @@ func init() {
pctx.SourcePathVariable("JlinkCmd", "${JavaToolchain}/jlink")
pctx.SourcePathVariable("JmodCmd", "${JavaToolchain}/jmod")
pctx.StaticVariable("Zip2ZipCmd", filepath.Join("${bootstrap.ToolDir}", "zip2zip"))
pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh")
pctx.StaticVariable("SoongZipCmd", filepath.Join("${bootstrap.ToolDir}", "soong_zip"))
pctx.StaticVariable("MergeZipsCmd", filepath.Join("${bootstrap.ToolDir}", "merge_zips"))
pctx.HostBinToolVariable("DxCmd", "dx")
pctx.HostJavaToolVariable("JarjarCmd", "jarjar.jar")

View File

@ -134,12 +134,6 @@ type Module struct {
// output file suitable for installing or running
outputFile android.Path
// jarSpecs suitable for inserting classes from a static library into another jar
classJarSpecs []jarSpec
// jarSpecs suitable for inserting resources from a static library into another jar
resourceJarSpecs []jarSpec
exportAidlIncludeDirs android.Paths
logtagsSrcs android.Paths
@ -154,8 +148,6 @@ type Module struct {
type Dependency interface {
ClasspathFiles() android.Paths
ClassJarSpecs() []jarSpec
ResourceJarSpecs() []jarSpec
AidlIncludeDirs() android.Paths
}
@ -229,9 +221,8 @@ func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.Opt
return flags
}
func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
func (j *Module) collectDeps(ctx android.ModuleContext) (classpath, bootClasspath, staticJars,
aidlIncludeDirs, srcFileLists android.Paths, aidlPreprocess android.OptionalPath) {
ctx.VisitDirectDeps(func(module blueprint.Module) {
otherName := ctx.OtherModuleName(module)
@ -254,8 +245,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths
classpath = append(classpath, dep.ClasspathFiles()...)
case staticLibTag:
classpath = append(classpath, dep.ClasspathFiles()...)
classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
staticJars = append(staticJars, dep.ClasspathFiles()...)
case frameworkResTag:
if ctx.ModuleName() == "framework" {
// framework.jar has a one-off dependency on the R.java and Manifest.java files
@ -280,16 +270,15 @@ func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths
aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
})
return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
aidlIncludeDirs, srcFileLists
return
}
func (j *Module) compile(ctx android.ModuleContext) {
j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
classpath, bootClasspath, staticJars, aidlIncludeDirs, srcFileLists,
aidlPreprocess := j.collectDeps(ctx)
var flags javaBuilderFlags
@ -341,6 +330,8 @@ func (j *Module) compile(ctx android.ModuleContext) {
var extraJarDeps android.Paths
var jars android.Paths
if len(srcFiles) > 0 {
// Compile java sources into .class files
classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
@ -359,37 +350,37 @@ func (j *Module) compile(ctx android.ModuleContext) {
extraJarDeps = append(extraJarDeps, errorprone)
}
classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
jars = append(jars, classes)
}
resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
resourceJarSpecs...)
resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs)
manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
if len(resourceJarSpecs) > 0 || manifest.Valid() {
// Combine classes + resources into classes-full-debug.jar
outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, manifest, extraJarDeps)
if ctx.Failed() {
return
}
jars = append(jars, resourceJar)
}
jars = append(jars, staticJars...)
// 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.
outputFile := TransformJarsToJar(ctx, "classes-combined.jar", jars)
if j.properties.Jarjar_rules != nil {
jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
// Transform classes-full-debug.jar into classes-jarjar.jar
// Transform classes-combined.jar into classes-jarjar.jar
outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
if ctx.Failed() {
return
}
classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
classJarSpecs = []jarSpec{classes}
}
j.resourceJarSpecs = resourceJarSpecs
j.classJarSpecs = classJarSpecs
j.classpathFile = outputFile
if j.deviceProperties.Dex && len(srcFiles) > 0 {
@ -447,14 +438,6 @@ func (j *Module) ClasspathFiles() android.Paths {
return android.Paths{j.classpathFile}
}
func (j *Module) ClassJarSpecs() []jarSpec {
return j.classJarSpecs
}
func (j *Module) ResourceJarSpecs() []jarSpec {
return j.resourceJarSpecs
}
func (j *Module) AidlIncludeDirs() android.Paths {
return j.exportAidlIncludeDirs
}
@ -582,7 +565,6 @@ type Import struct {
classpathFiles android.Paths
combinedClasspathFile android.Path
classJarSpecs, resourceJarSpecs []jarSpec
}
func (j *Import) Prebuilt() *android.Prebuilt {
@ -603,14 +585,7 @@ func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
for i, prebuilt := range j.classpathFiles {
subdir := "extracted" + strconv.Itoa(i)
classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
}
j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles)
}
var _ Dependency = (*Import)(nil)
@ -619,14 +594,6 @@ func (j *Import) ClasspathFiles() android.Paths {
return j.classpathFiles
}
func (j *Import) ClassJarSpecs() []jarSpec {
return j.classJarSpecs
}
func (j *Import) ResourceJarSpecs() []jarSpec {
return j.resourceJarSpecs
}
func (j *Import) AidlIncludeDirs() android.Paths {
return nil
}

View File

@ -69,6 +69,7 @@ func testJava(t *testing.T, bp string) *android.TestContext {
bp += fmt.Sprintf(`
java_library {
name: "%s",
srcs: ["a.java"],
no_standard_libs: true,
}
`, extra)
@ -112,20 +113,25 @@ func TestSimple(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "").Rule("javac")
jar := ctx.ModuleForTests("foo", "").Rule("jar")
combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
}
bar := filepath.Join(buildDir, ".intermediates", "bar", "classes-full-debug.jar")
bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar")
baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.jar")
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", "classes.list")
if !strings.Contains(jar.Args["jarArgs"], baz) {
t.Errorf("foo jarArgs %v does not contain %q", jar.Args["jarArgs"], baz)
if !strings.Contains(javac.Args["classpath"], baz) {
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz)
}
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
}
}
@ -176,7 +182,7 @@ func TestSdk(t *testing.T) {
check := func(module string, depType depType, deps ...string) {
for i := range deps {
deps[i] = filepath.Join(buildDir, ".intermediates", deps[i], "classes-full-debug.jar")
deps[i] = filepath.Join(buildDir, ".intermediates", deps[i], "classes.jar")
}
dep := strings.Join(deps, ":")
@ -229,16 +235,15 @@ func TestPrebuilts(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "").Rule("javac")
jar := ctx.ModuleForTests("foo", "").Rule("jar")
combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
bar := "a.jar"
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", "extracted0", "classes.list")
if !strings.Contains(jar.Args["jarArgs"], baz) {
t.Errorf("foo jarArgs %v does not contain %q", jar.Args["jarArgs"], baz)
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
}
}
@ -268,20 +273,20 @@ func TestDefaults(t *testing.T) {
`)
javac := ctx.ModuleForTests("foo", "").Rule("javac")
jar := ctx.ModuleForTests("foo", "").Rule("jar")
combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
}
bar := filepath.Join(buildDir, ".intermediates", "bar", "classes-full-debug.jar")
bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar")
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", "classes.list")
if !strings.Contains(jar.Args["jarArgs"], baz) {
t.Errorf("foo jarArgs %v does not contain %q", jar.Args["jarArgs"], baz)
baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.jar")
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
}
}

View File

@ -63,7 +63,7 @@ func ResourceDirsToJarSpecs(ctx android.ModuleContext, resourceDirs, excludeDirs
pattern := filepath.Join(dir.String(), "**/*")
bootstrap.GlobFile(ctx, pattern, excludes, fileListFile.String(), depFile)
jarSpecs = append(jarSpecs, TransformFileListToJarSpec(ctx, dir, fileListFile))
jarSpecs = append(jarSpecs, jarSpec{fileListFile, dir})
}
}