2017-12-23 07:56:08 +08:00
|
|
|
// Copyright 2017 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2020-05-21 05:20:28 +08:00
|
|
|
"github.com/google/blueprint/proptools"
|
2017-12-23 07:56:08 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-04-22 09:36:23 +08:00
|
|
|
"android/soong/remoteexec"
|
2017-12-23 07:56:08 +08:00
|
|
|
)
|
|
|
|
|
2020-05-21 02:27:16 +08:00
|
|
|
var d8, d8RE = remoteexec.MultiCommandStaticRules(pctx, "d8",
|
2017-12-23 07:56:08 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
2020-05-21 02:27:16 +08:00
|
|
|
`$d8Template${config.D8Cmd} ${config.DexFlags} --output $outDir $d8Flags $in && ` +
|
|
|
|
`$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
|
2018-07-15 23:16:31 +08:00
|
|
|
`${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
|
2017-12-23 07:56:08 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.D8Cmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2020-05-21 02:27:16 +08:00
|
|
|
}, map[string]*remoteexec.REParams{
|
|
|
|
"$d8Template": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "compile", "compiler": "d8"},
|
|
|
|
Inputs: []string{"${config.D8Jar}"},
|
|
|
|
ExecStrategy: "${config.RED8ExecStrategy}",
|
|
|
|
ToolchainInputs: []string{"${config.JavaCmd}"},
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
|
|
|
"$zipTemplate": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "tool", "name": "soong_zip"},
|
|
|
|
Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
|
|
|
|
OutputFiles: []string{"$outDir/classes.dex.jar"},
|
|
|
|
ExecStrategy: "${config.RED8ExecStrategy}",
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
2020-04-22 09:36:23 +08:00
|
|
|
}, []string{"outDir", "d8Flags", "zipFlags"}, nil)
|
|
|
|
|
2020-05-21 02:27:16 +08:00
|
|
|
var r8, r8RE = remoteexec.MultiCommandStaticRules(pctx, "r8",
|
2017-12-29 04:23:20 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
|
2018-08-22 22:16:23 +08:00
|
|
|
`rm -f "$outDict" && ` +
|
2020-05-21 02:27:16 +08:00
|
|
|
`$r8Template${config.R8Cmd} ${config.DexFlags} -injars $in --output $outDir ` +
|
2017-12-29 04:23:20 +08:00
|
|
|
`--force-proguard-compatibility ` +
|
2018-09-14 21:20:42 +08:00
|
|
|
`--no-data-resources ` +
|
2017-12-29 04:23:20 +08:00
|
|
|
`-printmapping $outDict ` +
|
2018-09-22 03:29:22 +08:00
|
|
|
`$r8Flags && ` +
|
2018-08-22 22:16:23 +08:00
|
|
|
`touch "$outDict" && ` +
|
2020-05-21 02:27:16 +08:00
|
|
|
`$zipTemplate${config.SoongZipCmd} $zipFlags -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
|
2018-07-15 23:16:31 +08:00
|
|
|
`${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
|
2017-12-29 04:23:20 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.R8Cmd}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2020-05-21 02:27:16 +08:00
|
|
|
}, map[string]*remoteexec.REParams{
|
|
|
|
"$r8Template": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "compile", "compiler": "r8"},
|
|
|
|
Inputs: []string{"$implicits", "${config.R8Jar}"},
|
|
|
|
ExecStrategy: "${config.RER8ExecStrategy}",
|
|
|
|
ToolchainInputs: []string{"${config.JavaCmd}"},
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
|
|
|
"$zipTemplate": &remoteexec.REParams{
|
|
|
|
Labels: map[string]string{"type": "tool", "name": "soong_zip"},
|
|
|
|
Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
|
|
|
|
OutputFiles: []string{"$outDir/classes.dex.jar"},
|
|
|
|
ExecStrategy: "${config.RER8ExecStrategy}",
|
|
|
|
Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
|
|
|
|
},
|
2020-04-22 09:36:23 +08:00
|
|
|
}, []string{"outDir", "outDict", "r8Flags", "zipFlags"}, []string{"implicits"})
|
2017-12-29 04:23:20 +08:00
|
|
|
|
2018-09-22 03:29:22 +08:00
|
|
|
func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
|
2017-12-23 07:56:08 +08:00
|
|
|
flags := j.deviceProperties.Dxflags
|
2018-06-07 05:46:32 +08:00
|
|
|
// Translate all the DX flags to D8 ones until all the build files have been migrated
|
|
|
|
// to D8 flags. See: b/69377755
|
|
|
|
flags = android.RemoveListFromList(flags,
|
|
|
|
[]string{"--core-library", "--dex", "--multi-dex"})
|
2017-12-23 07:56:08 +08:00
|
|
|
|
|
|
|
if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
|
2018-06-07 05:46:32 +08:00
|
|
|
flags = append(flags, "--debug")
|
2017-12-23 07:56:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
|
|
|
|
flags = append(flags,
|
|
|
|
"--debug",
|
|
|
|
"--verbose")
|
|
|
|
}
|
|
|
|
|
Abstract sdk_version string using sdkSpec type
The value format that sdk_version (and min_sdk_version, etc.) can have
has consistently evolved and is quite complicated. Furthermore, with the
Mainline module effort, we are expected to have more sdk_versions like
'module-app-current', 'module-lib-current', etc.
The goal of this change is to abstract the various sdk versions, which
are currently represented in string and is parsed in various places,
into a type called sdkSpec, so that adding new sdk veresions becomes
easier than before.
The sdk_version string is now parsed in only one place 'SdkSpecFrom', in
which it is converted into the sdkSpec struct. The struct type provides
several methods that again converts sdkSpec into context-specific
information such as the effective version number, etc.
Bug: 146757305
Bug: 147879031
Test: m
Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0
2020-01-21 01:03:43 +08:00
|
|
|
minSdkVersion, err := j.minSdkVersion().effectiveVersion(ctx)
|
2018-06-26 06:48:06 +08:00
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("min_sdk_version", "%s", err)
|
|
|
|
}
|
|
|
|
|
Abstract sdk_version string using sdkSpec type
The value format that sdk_version (and min_sdk_version, etc.) can have
has consistently evolved and is quite complicated. Furthermore, with the
Mainline module effort, we are expected to have more sdk_versions like
'module-app-current', 'module-lib-current', etc.
The goal of this change is to abstract the various sdk versions, which
are currently represented in string and is parsed in various places,
into a type called sdkSpec, so that adding new sdk veresions becomes
easier than before.
The sdk_version string is now parsed in only one place 'SdkSpecFrom', in
which it is converted into the sdkSpec struct. The struct type provides
several methods that again converts sdkSpec into context-specific
information such as the effective version number, etc.
Bug: 146757305
Bug: 147879031
Test: m
Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0
2020-01-21 01:03:43 +08:00
|
|
|
flags = append(flags, "--min-api "+minSdkVersion.asNumberString())
|
2017-12-23 07:56:08 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2018-09-28 23:06:24 +08:00
|
|
|
func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
|
2018-09-22 03:29:22 +08:00
|
|
|
d8Flags := j.dexCommonFlags(ctx)
|
|
|
|
|
2019-11-01 06:22:57 +08:00
|
|
|
d8Flags = append(d8Flags, flags.bootClasspath.FormRepeatedClassPath("--lib ")...)
|
|
|
|
d8Flags = append(d8Flags, flags.classpath.FormRepeatedClassPath("--lib ")...)
|
2018-09-22 03:29:22 +08:00
|
|
|
|
2018-09-28 23:06:24 +08:00
|
|
|
var d8Deps android.Paths
|
|
|
|
d8Deps = append(d8Deps, flags.bootClasspath...)
|
|
|
|
d8Deps = append(d8Deps, flags.classpath...)
|
|
|
|
|
|
|
|
return d8Flags, d8Deps
|
2018-09-22 03:29:22 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
|
|
|
|
opt := j.deviceProperties.Optimize
|
|
|
|
|
|
|
|
// When an app contains references to APIs that are not in the SDK specified by
|
|
|
|
// its LOCAL_SDK_VERSION for example added by support library or by runtime
|
2018-06-07 05:46:32 +08:00
|
|
|
// classes added by desugaring, we artifically raise the "SDK version" "linked" by
|
2017-12-29 04:23:20 +08:00
|
|
|
// ProGuard, to
|
|
|
|
// - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
|
|
|
|
// - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
|
|
|
|
// See b/20667396
|
|
|
|
var proguardRaiseDeps classpath
|
|
|
|
ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(dep android.Module) {
|
|
|
|
proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
|
|
|
|
})
|
|
|
|
|
2018-09-22 03:29:22 +08:00
|
|
|
r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
|
|
|
|
r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
|
|
|
|
r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
|
|
|
|
|
2018-09-28 23:06:24 +08:00
|
|
|
r8Deps = append(r8Deps, proguardRaiseDeps...)
|
|
|
|
r8Deps = append(r8Deps, flags.bootClasspath...)
|
|
|
|
r8Deps = append(r8Deps, flags.classpath...)
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
flagFiles := android.Paths{
|
|
|
|
android.PathForSource(ctx, "build/make/core/proguard.flags"),
|
|
|
|
}
|
|
|
|
|
2018-01-04 07:06:47 +08:00
|
|
|
if j.shouldInstrumentStatic(ctx) {
|
|
|
|
flagFiles = append(flagFiles,
|
|
|
|
android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
|
|
|
|
// TODO(ccross): static android library proguard files
|
|
|
|
|
2018-08-14 01:28:18 +08:00
|
|
|
flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, j.deviceProperties.Optimize.Proguard_flags_files)...)
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))
|
|
|
|
r8Deps = append(r8Deps, flagFiles...)
|
|
|
|
|
|
|
|
// TODO(b/70942988): This is included from build/make/core/proguard.flags
|
|
|
|
r8Deps = append(r8Deps, android.PathForSource(ctx,
|
|
|
|
"build/make/core/proguard_basic_keeps.flags"))
|
|
|
|
|
|
|
|
r8Flags = append(r8Flags, j.deviceProperties.Optimize.Proguard_flags...)
|
|
|
|
|
|
|
|
// TODO(ccross): Don't shrink app instrumentation tests by default.
|
|
|
|
if !Bool(opt.Shrink) {
|
|
|
|
r8Flags = append(r8Flags, "-dontshrink")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !Bool(opt.Optimize) {
|
|
|
|
r8Flags = append(r8Flags, "-dontoptimize")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ccross): error if obufscation + app instrumentation test.
|
|
|
|
if !Bool(opt.Obfuscate) {
|
|
|
|
r8Flags = append(r8Flags, "-dontobfuscate")
|
|
|
|
}
|
2018-10-16 07:18:06 +08:00
|
|
|
// TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
|
|
|
|
// dictionary of the app and move the app from libraryjars to injars.
|
2017-12-29 04:23:20 +08:00
|
|
|
|
2018-11-30 07:08:44 +08:00
|
|
|
// Don't strip out debug information for eng builds.
|
|
|
|
if ctx.Config().Eng() {
|
|
|
|
r8Flags = append(r8Flags, "--debug")
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
return r8Flags, r8Deps
|
|
|
|
}
|
|
|
|
|
2017-12-23 07:56:08 +08:00
|
|
|
func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
|
2018-08-16 02:19:12 +08:00
|
|
|
classesJar android.Path, jarName string) android.ModuleOutPath {
|
2017-12-23 07:56:08 +08:00
|
|
|
|
2019-04-17 08:16:58 +08:00
|
|
|
useR8 := j.deviceProperties.EffectiveOptimizeEnabled()
|
2017-12-23 07:56:08 +08:00
|
|
|
|
|
|
|
// Compile classes.jar into classes.dex and then javalib.jar
|
|
|
|
javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
|
|
|
|
outDir := android.PathForModuleOut(ctx, "dex")
|
|
|
|
|
2019-02-16 02:14:23 +08:00
|
|
|
zipFlags := "--ignore_missing_files"
|
2020-05-21 05:20:28 +08:00
|
|
|
if proptools.Bool(j.deviceProperties.Uncompress_dex) {
|
2019-02-16 02:14:23 +08:00
|
|
|
zipFlags += " -L 0"
|
2018-10-06 05:20:06 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 04:23:20 +08:00
|
|
|
if useR8 {
|
2018-05-17 07:47:21 +08:00
|
|
|
proguardDictionary := android.PathForModuleOut(ctx, "proguard_dictionary")
|
|
|
|
j.proguardDictionary = proguardDictionary
|
2017-12-29 04:23:20 +08:00
|
|
|
r8Flags, r8Deps := j.r8Flags(ctx, flags)
|
2020-04-22 09:36:23 +08:00
|
|
|
rule := r8
|
|
|
|
args := map[string]string{
|
|
|
|
"r8Flags": strings.Join(r8Flags, " "),
|
|
|
|
"zipFlags": zipFlags,
|
|
|
|
"outDict": j.proguardDictionary.String(),
|
|
|
|
"outDir": outDir.String(),
|
|
|
|
}
|
|
|
|
if ctx.Config().IsEnvTrue("RBE_R8") {
|
|
|
|
rule = r8RE
|
|
|
|
args["implicits"] = strings.Join(r8Deps.Strings(), ",")
|
|
|
|
}
|
2017-12-29 04:23:20 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-04-22 09:36:23 +08:00
|
|
|
Rule: rule,
|
2018-05-17 07:47:21 +08:00
|
|
|
Description: "r8",
|
|
|
|
Output: javalibJar,
|
|
|
|
ImplicitOutput: proguardDictionary,
|
|
|
|
Input: classesJar,
|
|
|
|
Implicits: r8Deps,
|
2020-04-22 09:36:23 +08:00
|
|
|
Args: args,
|
2017-12-29 04:23:20 +08:00
|
|
|
})
|
|
|
|
} else {
|
2018-09-28 23:06:24 +08:00
|
|
|
d8Flags, d8Deps := j.d8Flags(ctx, flags)
|
2020-04-22 09:36:23 +08:00
|
|
|
rule := d8
|
|
|
|
if ctx.Config().IsEnvTrue("RBE_D8") {
|
|
|
|
rule = d8RE
|
|
|
|
}
|
2017-12-29 04:23:20 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-04-22 09:36:23 +08:00
|
|
|
Rule: rule,
|
2018-06-07 05:46:32 +08:00
|
|
|
Description: "d8",
|
2017-12-29 04:23:20 +08:00
|
|
|
Output: javalibJar,
|
|
|
|
Input: classesJar,
|
2018-09-28 23:06:24 +08:00
|
|
|
Implicits: d8Deps,
|
2017-12-29 04:23:20 +08:00
|
|
|
Args: map[string]string{
|
2018-10-06 05:20:06 +08:00
|
|
|
"d8Flags": strings.Join(d8Flags, " "),
|
|
|
|
"zipFlags": zipFlags,
|
|
|
|
"outDir": outDir.String(),
|
2017-12-29 04:23:20 +08:00
|
|
|
},
|
|
|
|
})
|
2017-12-23 07:56:08 +08:00
|
|
|
}
|
2020-05-21 05:20:28 +08:00
|
|
|
if proptools.Bool(j.deviceProperties.Uncompress_dex) {
|
2019-01-22 07:20:23 +08:00
|
|
|
alignedJavalibJar := android.PathForModuleOut(ctx, "aligned", jarName)
|
|
|
|
TransformZipAlign(ctx, alignedJavalibJar, javalibJar)
|
|
|
|
javalibJar = alignedJavalibJar
|
|
|
|
}
|
2017-12-23 07:56:08 +08:00
|
|
|
|
|
|
|
return javalibJar
|
|
|
|
}
|