2015-03-31 08:20:39 +08:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
// This file generates the final rules for compiling all Java. All properties related to
|
|
|
|
// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
|
|
|
|
// functions.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
"android/soong/android"
|
2015-03-31 08:20:39 +08:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2015-07-16 05:34:02 +08:00
|
|
|
_ "github.com/google/blueprint/bootstrap"
|
2015-03-31 08:20:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-19 06:37:25 +08:00
|
|
|
pctx = android.NewPackageContext("android/soong/java")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
|
|
|
// Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
|
|
|
|
// requirement leads to unpredictable generated source file names, and a single .java file
|
|
|
|
// will get compiled into multiple .class files if it contains inner classes. To work around
|
|
|
|
// this, all java rules write into separate directories and then a post-processing step lists
|
|
|
|
// the files in the the directory into a list file that later rules depend on (and sometimes
|
|
|
|
// read from directly using @<listfile>)
|
2015-04-11 06:41:49 +08:00
|
|
|
javac = pctx.StaticRule("javac",
|
2015-03-31 08:20:39 +08:00
|
|
|
blueprint.RuleParams{
|
2015-04-11 06:41:49 +08:00
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
|
|
|
`$javacCmd -encoding UTF-8 $javacFlags $bootClasspath $classpath ` +
|
|
|
|
`-extdirs "" -d $outDir @$out.rsp || ( rm -rf "$outDir"; exit 41 ) && ` +
|
2015-03-31 08:20:39 +08:00
|
|
|
`find $outDir -name "*.class" > $out`,
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
Description: "javac $outDir",
|
|
|
|
},
|
|
|
|
"javacCmd", "javacFlags", "bootClasspath", "classpath", "outDir")
|
|
|
|
|
|
|
|
jar = pctx.StaticRule("jar",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `$jarCmd -o $out $jarArgs`,
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$jarCmd"},
|
2015-03-31 08:20:39 +08:00
|
|
|
Description: "jar $out",
|
|
|
|
},
|
|
|
|
"jarCmd", "jarArgs")
|
|
|
|
|
|
|
|
dx = pctx.StaticRule("dx",
|
|
|
|
blueprint.RuleParams{
|
2015-04-11 08:44:24 +08:00
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
|
|
|
`$dxCmd --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
|
|
|
|
`find "$outDir" -name "classes*.dex" > $out`,
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$dxCmd"},
|
2015-03-31 08:20:39 +08:00
|
|
|
Description: "dex $out",
|
|
|
|
},
|
|
|
|
"outDir", "dxFlags")
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2015-04-04 07:54:17 +08:00
|
|
|
jarjar = pctx.StaticRule("jarjar",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "java -jar $jarjarCmd process $rulesFile $in $out",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$jarjarCmd", "$rulesFile"},
|
2015-04-04 07:54:17 +08:00
|
|
|
Description: "jarjar $out",
|
|
|
|
},
|
|
|
|
"rulesFile")
|
|
|
|
|
2015-04-04 07:53:05 +08:00
|
|
|
extractPrebuilt = pctx.StaticRule("extractPrebuilt",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf $outDir && unzip -qo $in -d $outDir && ` +
|
|
|
|
`find $outDir -name "*.class" > $classFile && ` +
|
|
|
|
`find $outDir -type f -a \! -name "*.class" -a \! -name "MANIFEST.MF" > $resourceFile || ` +
|
|
|
|
`(rm -rf $outDir; exit 42)`,
|
|
|
|
Description: "extract java prebuilt $outDir",
|
|
|
|
},
|
|
|
|
"outDir", "classFile", "resourceFile")
|
2015-03-31 08:20:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-07-16 05:34:02 +08:00
|
|
|
pctx.Import("github.com/google/blueprint/bootstrap")
|
2015-03-31 08:20:39 +08:00
|
|
|
pctx.StaticVariable("commonJdkFlags", "-source 1.7 -target 1.7 -Xmaxerrs 9999999")
|
|
|
|
pctx.StaticVariable("javacCmd", "javac -J-Xmx1024M $commonJdkFlags")
|
2016-08-11 07:12:30 +08:00
|
|
|
pctx.StaticVariable("jarCmd", filepath.Join("${bootstrap.ToolDir}", "soong_zip"))
|
2015-09-24 06:26:20 +08:00
|
|
|
pctx.HostBinToolVariable("dxCmd", "dx")
|
|
|
|
pctx.HostJavaToolVariable("jarjarCmd", "jarjar.jar")
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type javaBuilderFlags struct {
|
|
|
|
javacFlags string
|
|
|
|
dxFlags string
|
|
|
|
bootClasspath string
|
|
|
|
classpath string
|
2015-04-09 04:03:43 +08:00
|
|
|
aidlFlags string
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type jarSpec struct {
|
2016-05-19 06:37:25 +08:00
|
|
|
fileList, dir android.Path
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j jarSpec) soongJarArgs() string {
|
2015-09-24 06:26:20 +08:00
|
|
|
return "-C " + j.dir.String() + " -l " + j.fileList.String()
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformJavaToClasses(ctx android.ModuleContext, srcFiles android.Paths, srcFileLists android.Paths,
|
|
|
|
flags javaBuilderFlags, deps android.Paths) jarSpec {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
classDir := android.PathForModuleOut(ctx, "classes")
|
|
|
|
classFileList := android.PathForModuleOut(ctx, "classes.list")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
javacFlags := flags.javacFlags + android.JoinWithPrefix(srcFileLists.Strings(), "@")
|
2015-04-14 05:02:52 +08:00
|
|
|
|
|
|
|
deps = append(deps, srcFileLists...)
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-04-11 06:41:49 +08:00
|
|
|
Rule: javac,
|
2015-09-24 06:26:20 +08:00
|
|
|
Output: classFileList,
|
2015-03-31 08:20:39 +08:00
|
|
|
Inputs: srcFiles,
|
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
2015-04-14 05:02:52 +08:00
|
|
|
"javacFlags": javacFlags,
|
2015-03-31 08:20:39 +08:00
|
|
|
"bootClasspath": flags.bootClasspath,
|
|
|
|
"classpath": flags.classpath,
|
2015-09-24 06:26:20 +08:00
|
|
|
"outDir": classDir.String(),
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return jarSpec{classFileList, classDir}
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformClassesToJar(ctx android.ModuleContext, classes []jarSpec,
|
|
|
|
manifest android.OptionalPath) android.Path {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
outputFile := android.PathForModuleOut(ctx, "classes-full-debug.jar")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
deps := android.Paths{}
|
2015-03-31 08:20:39 +08:00
|
|
|
jarArgs := []string{}
|
|
|
|
|
|
|
|
for _, j := range classes {
|
|
|
|
deps = append(deps, j.fileList)
|
|
|
|
jarArgs = append(jarArgs, j.soongJarArgs())
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
if manifest.Valid() {
|
|
|
|
deps = append(deps, manifest.Path())
|
|
|
|
jarArgs = append(jarArgs, "-m "+manifest.String())
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-03-31 08:20:39 +08:00
|
|
|
Rule: jar,
|
2015-09-24 06:26:20 +08:00
|
|
|
Output: outputFile,
|
2015-03-31 08:20:39 +08:00
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"jarArgs": strings.Join(jarArgs, " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return outputFile
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformClassesJarToDex(ctx android.ModuleContext, classesJar android.Path,
|
2015-04-11 08:44:24 +08:00
|
|
|
flags javaBuilderFlags) jarSpec {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
outDir := android.PathForModuleOut(ctx, "dex")
|
|
|
|
outputFile := android.PathForModuleOut(ctx, "dex.filelist")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-09-24 06:26:20 +08:00
|
|
|
Rule: dx,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: classesJar,
|
2015-03-31 08:20:39 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"dxFlags": flags.dxFlags,
|
2015-09-24 06:26:20 +08:00
|
|
|
"outDir": outDir.String(),
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-04-11 08:44:24 +08:00
|
|
|
return jarSpec{outputFile, outDir}
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformDexToJavaLib(ctx android.ModuleContext, resources []jarSpec,
|
|
|
|
dexJarSpec jarSpec) android.Path {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
outputFile := android.PathForModuleOut(ctx, "javalib.jar")
|
|
|
|
var deps android.Paths
|
2015-03-31 08:20:39 +08:00
|
|
|
var jarArgs []string
|
|
|
|
|
|
|
|
for _, j := range resources {
|
|
|
|
deps = append(deps, j.fileList)
|
|
|
|
jarArgs = append(jarArgs, j.soongJarArgs())
|
|
|
|
}
|
|
|
|
|
2015-04-11 08:44:24 +08:00
|
|
|
deps = append(deps, dexJarSpec.fileList)
|
|
|
|
jarArgs = append(jarArgs, dexJarSpec.soongJarArgs())
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-03-31 08:20:39 +08:00
|
|
|
Rule: jar,
|
2015-09-24 06:26:20 +08:00
|
|
|
Output: outputFile,
|
2015-03-31 08:20:39 +08:00
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"jarArgs": strings.Join(jarArgs, " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return outputFile
|
|
|
|
}
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.Path {
|
|
|
|
outputFile := android.PathForModuleOut(ctx, "classes-jarjar.jar")
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-09-24 06:26:20 +08:00
|
|
|
Rule: jarjar,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: classesJar,
|
|
|
|
Implicit: rulesFile,
|
2015-04-04 07:54:17 +08:00
|
|
|
Args: map[string]string{
|
2015-09-24 06:26:20 +08:00
|
|
|
"rulesFile": rulesFile.String(),
|
2015-04-04 07:54:17 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return outputFile
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformPrebuiltJarToClasses(ctx android.ModuleContext,
|
|
|
|
prebuilt android.Path) (classJarSpec, resourceJarSpec jarSpec) {
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
classDir := android.PathForModuleOut(ctx, "extracted/classes")
|
|
|
|
classFileList := android.PathForModuleOut(ctx, "extracted/classes.list")
|
|
|
|
resourceFileList := android.PathForModuleOut(ctx, "extracted/resources.list")
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-04-04 07:53:05 +08:00
|
|
|
Rule: extractPrebuilt,
|
2016-05-19 06:37:25 +08:00
|
|
|
Outputs: android.WritablePaths{classFileList, resourceFileList},
|
2015-09-24 06:26:20 +08:00
|
|
|
Input: prebuilt,
|
2015-04-04 07:53:05 +08:00
|
|
|
Args: map[string]string{
|
2015-09-24 06:26:20 +08:00
|
|
|
"outDir": classDir.String(),
|
|
|
|
"classFile": classFileList.String(),
|
|
|
|
"resourceFile": resourceFileList.String(),
|
2015-04-04 07:53:05 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return jarSpec{classFileList, classDir}, jarSpec{resourceFileList, classDir}
|
|
|
|
}
|