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 (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2017-09-06 04:16:45 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/java/config"
|
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>)
|
2017-06-05 16:41:50 +08:00
|
|
|
javac = pctx.AndroidGomaStaticRule("javac",
|
2015-03-31 08:20:39 +08:00
|
|
|
blueprint.RuleParams{
|
2017-08-09 04:17:59 +08:00
|
|
|
Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
|
2017-08-29 08:23:21 +08:00
|
|
|
`${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
|
2017-08-09 04:17:59 +08:00
|
|
|
`$javacFlags $bootClasspath $classpath ` +
|
|
|
|
`-source $javaVersion -target $javaVersion ` +
|
2017-08-11 08:58:12 +08:00
|
|
|
`-d $outDir -s $annoDir @$out.rsp && ` +
|
2017-08-31 05:24:55 +08:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
|
|
|
|
CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"},
|
2015-03-31 08:20:39 +08:00
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
2017-08-09 04:17:59 +08:00
|
|
|
"javacFlags", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2017-08-15 05:16:06 +08:00
|
|
|
errorprone = pctx.AndroidStaticRule("errorprone",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
|
|
|
|
`${config.ErrorProneCmd}` +
|
|
|
|
`$javacFlags $bootClasspath $classpath ` +
|
|
|
|
`-source $javaVersion -target $javaVersion ` +
|
|
|
|
`-d $outDir -s $annoDir @$out.rsp && ` +
|
2017-08-31 05:24:55 +08:00
|
|
|
`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
|
2017-08-15 05:16:06 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.JavaCmd}",
|
|
|
|
"${config.ErrorProneJavacJar}",
|
|
|
|
"${config.ErrorProneJar}",
|
2017-08-31 05:24:55 +08:00
|
|
|
"${config.SoongZipCmd}",
|
2017-08-15 05:16:06 +08:00
|
|
|
},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
|
|
|
"javacFlags", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
|
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
jar = pctx.AndroidStaticRule("jar",
|
2015-03-31 08:20:39 +08:00
|
|
|
blueprint.RuleParams{
|
2017-08-31 05:24:55 +08:00
|
|
|
Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
|
|
|
|
CommandDeps: []string{"${config.SoongZipCmd}"},
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
2017-08-31 05:24:55 +08:00
|
|
|
"jarArgs")
|
|
|
|
|
|
|
|
combineJar = pctx.AndroidStaticRule("combineJar",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `${config.MergeZipsCmd} -j $out $in`,
|
|
|
|
CommandDeps: []string{"${config.MergeZipsCmd}"},
|
|
|
|
},
|
|
|
|
"outDir")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
dx = pctx.AndroidStaticRule("dx",
|
2015-03-31 08:20:39 +08:00
|
|
|
blueprint.RuleParams{
|
2015-04-11 08:44:24 +08:00
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
2017-08-31 05:24:55 +08:00
|
|
|
`${config.DxCmd} --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
|
|
|
|
`find "$outDir" -name "classes*.dex" | sort > $out`,
|
|
|
|
CommandDeps: []string{"${config.DxCmd}"},
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
"outDir", "dxFlags")
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
jarjar = pctx.AndroidStaticRule("jarjar",
|
2015-04-04 07:54:17 +08:00
|
|
|
blueprint.RuleParams{
|
2017-08-09 04:17:59 +08:00
|
|
|
Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
|
|
|
|
CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
|
2015-04-04 07:54:17 +08:00
|
|
|
},
|
|
|
|
"rulesFile")
|
2015-03-31 08:20:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-08-09 04:17:59 +08:00
|
|
|
pctx.Import("android/soong/java/config")
|
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
|
2017-08-09 04:17:59 +08:00
|
|
|
javaVersion string
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type jarSpec struct {
|
2017-08-31 05:24:55 +08:00
|
|
|
fileList, dir android.Path
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
func (j jarSpec) soongJarArgs() string {
|
|
|
|
return "-C " + j.dir.String() + " -l " + j.fileList.String()
|
2017-08-11 08:58:12 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
func TransformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
|
|
|
|
flags javaBuilderFlags, deps android.Paths) android.ModuleOutPath {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
classDir := android.PathForModuleOut(ctx, "classes")
|
2017-08-09 04:17:59 +08:00
|
|
|
annoDir := android.PathForModuleOut(ctx, "anno")
|
2017-08-31 05:24:55 +08:00
|
|
|
classJar := android.PathForModuleOut(ctx, "classes.jar")
|
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{
|
2017-05-10 04:45:28 +08:00
|
|
|
Rule: javac,
|
|
|
|
Description: "javac",
|
2017-08-31 05:24:55 +08:00
|
|
|
Output: classJar,
|
2017-05-10 04:45:28 +08:00
|
|
|
Inputs: srcFiles,
|
|
|
|
Implicits: deps,
|
2015-03-31 08:20:39 +08:00
|
|
|
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(),
|
2017-08-09 04:17:59 +08:00
|
|
|
"annoDir": annoDir.String(),
|
|
|
|
"javaVersion": flags.javaVersion,
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
return classJar
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 05:16:06 +08:00
|
|
|
func RunErrorProne(ctx android.ModuleContext, srcFiles android.Paths, srcFileLists android.Paths,
|
|
|
|
flags javaBuilderFlags, deps android.Paths) android.Path {
|
|
|
|
|
2017-09-06 04:16:45 +08:00
|
|
|
if config.ErrorProneJar == "" {
|
|
|
|
ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-15 05:16:06 +08:00
|
|
|
classDir := android.PathForModuleOut(ctx, "classes-errorprone")
|
|
|
|
annoDir := android.PathForModuleOut(ctx, "anno-errorprone")
|
|
|
|
classFileList := android.PathForModuleOut(ctx, "classes-errorprone.list")
|
|
|
|
|
|
|
|
javacFlags := flags.javacFlags + android.JoinWithPrefix(srcFileLists.Strings(), "@")
|
|
|
|
|
|
|
|
deps = append(deps, srcFileLists...)
|
|
|
|
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
|
|
|
Rule: errorprone,
|
|
|
|
Description: "errorprone",
|
|
|
|
Output: classFileList,
|
|
|
|
Inputs: srcFiles,
|
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"javacFlags": javacFlags,
|
|
|
|
"bootClasspath": flags.bootClasspath,
|
|
|
|
"classpath": flags.classpath,
|
|
|
|
"outDir": classDir.String(),
|
|
|
|
"annoDir": annoDir.String(),
|
|
|
|
"javaVersion": flags.javaVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return classFileList
|
|
|
|
}
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
func TransformResourcesToJar(ctx android.ModuleContext, resources []jarSpec,
|
2017-08-15 05:16:06 +08:00
|
|
|
manifest android.OptionalPath, deps android.Paths) android.Path {
|
2015-03-31 08:20:39 +08:00
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
outputFile := android.PathForModuleOut(ctx, "res.jar")
|
2015-03-31 08:20:39 +08:00
|
|
|
|
|
|
|
jarArgs := []string{}
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
for _, j := range resources {
|
|
|
|
deps = append(deps, j.fileList)
|
|
|
|
jarArgs = append(jarArgs, j.soongJarArgs())
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
if manifest.Valid() {
|
|
|
|
deps = append(deps, manifest.Path())
|
2017-08-31 05:24:55 +08:00
|
|
|
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{
|
2017-05-10 04:45:28 +08:00
|
|
|
Rule: jar,
|
|
|
|
Description: "jar",
|
|
|
|
Output: outputFile,
|
|
|
|
Implicits: deps,
|
2015-03-31 08:20:39 +08:00
|
|
|
Args: map[string]string{
|
2017-08-31 05:24:55 +08:00
|
|
|
"jarArgs": strings.Join(jarArgs, " "),
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return outputFile
|
|
|
|
}
|
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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{
|
2017-05-10 04:45:28 +08:00
|
|
|
Rule: dx,
|
|
|
|
Description: "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
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-08-31 05:24:55 +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 {
|
2017-08-31 05:24:55 +08:00
|
|
|
deps = append(deps, j.fileList)
|
|
|
|
jarArgs = append(jarArgs, j.soongJarArgs())
|
2015-03-31 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 05:24:55 +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{
|
2017-05-10 04:45:28 +08:00
|
|
|
Rule: jar,
|
|
|
|
Description: "jar",
|
|
|
|
Output: outputFile,
|
|
|
|
Implicits: deps,
|
2015-03-31 08:20:39 +08:00
|
|
|
Args: map[string]string{
|
2017-08-31 05:24:55 +08:00
|
|
|
"jarArgs": strings.Join(jarArgs, " "),
|
2015-03-31 08:20:39 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return outputFile
|
|
|
|
}
|
2015-04-04 07:53:05 +08:00
|
|
|
|
2017-08-31 05:24:55 +08:00
|
|
|
func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.ModuleOutPath {
|
2016-05-19 06:37:25 +08:00
|
|
|
outputFile := android.PathForModuleOut(ctx, "classes-jarjar.jar")
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2017-05-10 04:45:28 +08:00
|
|
|
Rule: jarjar,
|
|
|
|
Description: "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
|
|
|
|
}
|