summaryrefslogtreecommitdiff
path: root/java/java.go
diff options
context:
space:
mode:
Diffstat (limited to 'java/java.go')
-rw-r--r--java/java.go220
1 files changed, 139 insertions, 81 deletions
diff --git a/java/java.go b/java/java.go
index 659f98a7c..0841dad5e 100644
--- a/java/java.go
+++ b/java/java.go
@@ -517,14 +517,8 @@ func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext an
return normalizeJavaVersion(ctx, javaVersion)
} else if ctx.Device() {
return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
- } else if ctx.Config().TargetsJava17() {
- // Temporary experimental flag to be able to try and build with
- // java version 17 options. The flag, if used, just sets Java
- // 17 as the default version, leaving any components that
- // target an older version intact.
- return JAVA_VERSION_17
} else {
- return JAVA_VERSION_11
+ return JAVA_VERSION_17
}
}
@@ -801,6 +795,8 @@ type librarySdkMemberProperties struct {
// The value of the min_sdk_version property, translated into a number where possible.
MinSdkVersion *string `supported_build_releases:"Tiramisu+"`
+
+ DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
}
func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
@@ -818,6 +814,10 @@ func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberCo
canonical := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.ApiLevel.String())
p.MinSdkVersion = proptools.StringPtr(canonical)
}
+
+ if j.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
+ p.DexPreoptProfileGuided = proptools.BoolPtr(true)
+ }
}
func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
@@ -844,6 +844,11 @@ func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberConte
propertySet.AddProperty("permitted_packages", p.PermittedPackages)
}
+ dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
+ if p.DexPreoptProfileGuided != nil {
+ dexPreoptSet.AddProperty("profile_guided", proptools.Bool(p.DexPreoptProfileGuided))
+ }
+
// Do not copy anything else to the snapshot.
if memberType.onlyCopyJarToSnapshot {
return
@@ -1633,6 +1638,10 @@ type JavaApiLibraryProperties struct {
// List of shared java libs that this module has dependencies to and
// should be passed as classpath in javac invocation
Libs []string
+
+ // List of java libs that this module has static dependencies to and will be
+ // passed in metalava invocation
+ Static_libs []string
}
func ApiLibraryFactory() android.Module {
@@ -1705,6 +1714,7 @@ func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName)
}
ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...)
+ ctx.AddVariationDependencies(nil, staticLibTag, al.properties.Static_libs...)
}
func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -1724,6 +1734,7 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
var srcFiles android.Paths
var classPaths android.Paths
+ var staticLibs android.Paths
ctx.VisitDirectDeps(func(dep android.Module) {
tag := ctx.OtherModuleDependencyTag(dep)
switch tag {
@@ -1737,6 +1748,9 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
case libTag:
provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
classPaths = append(classPaths, provider.HeaderJars...)
+ case staticLibTag:
+ provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
+ staticLibs = append(staticLibs, provider.HeaderJars...)
}
})
@@ -1761,17 +1775,25 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
FlagWithArg("-D ", stubsDir.String())
rule.Build("metalava", "metalava merged")
-
- al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), "android.jar")
+ compiledStubs := android.PathForModuleOut(ctx, ctx.ModuleName(), "stubs.jar")
+ al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), fmt.Sprintf("%s.jar", ctx.ModuleName()))
var flags javaBuilderFlags
flags.javaVersion = getStubsJavaVersion()
flags.javacFlags = strings.Join(al.properties.Javacflags, " ")
flags.classpath = classpath(classPaths)
- TransformJavaToClasses(ctx, al.stubsJar, 0, android.Paths{},
+ TransformJavaToClasses(ctx, compiledStubs, 0, android.Paths{},
android.Paths{al.stubsSrcJar}, flags, android.Paths{})
+ builder := android.NewRuleBuilder(pctx, ctx)
+ builder.Command().
+ BuiltTool("merge_zips").
+ Output(al.stubsJar).
+ Inputs(android.Paths{compiledStubs}).
+ Inputs(staticLibs)
+ builder.Build("merge_zips", "merge jar files")
+
ctx.Phony(ctx.ModuleName(), al.stubsJar)
ctx.SetProvider(JavaInfoProvider, JavaInfo{
@@ -1997,7 +2019,8 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
if di == nil {
return // An error has been reported by FindDeapexerProviderForModule.
}
- if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
+ dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(j.BaseModuleName())
+ if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
dexJarFile := makeDexJarPathFromPath(dexOutputPath)
j.dexJarFile = dexJarFile
installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
@@ -2006,6 +2029,11 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
+
+ if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
+ j.dexpreopter.inputProfilePathOnHost = profilePath
+ }
+
j.dexpreopt(ctx, dexOutputPath)
// Initialize the hiddenapi structure.
@@ -2033,7 +2061,15 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
var dexOutputFile android.OutputPath
- dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
+ dexParams := &compileDexParams{
+ flags: flags,
+ sdkVersion: j.SdkVersion(ctx),
+ minSdkVersion: j.MinSdkVersion(ctx),
+ classesJar: outputFile,
+ jarName: jarName,
+ }
+
+ dexOutputFile = j.dexer.compileDex(ctx, dexParams)
if ctx.Failed() {
return
}
@@ -2124,15 +2160,18 @@ func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Modu
// Implements android.ApexModule
func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
sdkVersion android.ApiLevel) error {
- sdkSpec := j.MinSdkVersion(ctx)
- if !sdkSpec.Specified() {
+ sdkVersionSpec := j.SdkVersion(ctx)
+ minSdkVersionSpec := j.MinSdkVersion(ctx)
+ if !minSdkVersionSpec.Specified() {
return fmt.Errorf("min_sdk_version is not specified")
}
- if sdkSpec.Kind == android.SdkCore {
+ // If the module is compiling against core (via sdk_version), skip comparison check.
+ if sdkVersionSpec.Kind == android.SdkCore {
return nil
}
- if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
- return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
+ minSdkVersion := minSdkVersionSpec.ApiLevel
+ if minSdkVersion.GreaterThan(sdkVersion) {
+ return fmt.Errorf("newer SDK(%v)", minSdkVersion)
}
return nil
}
@@ -2140,11 +2179,16 @@ func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
// java_sdk_library_import with the specified base module name requires to be exported from a
// prebuilt_apex/apex_set.
-func requiredFilesFromPrebuiltApexForImport(name string) []string {
+func requiredFilesFromPrebuiltApexForImport(name string, d *dexpreopter) []string {
+ dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(name)
// Add the dex implementation jar to the set of exported files.
- return []string{
- apexRootRelativePathToJavaLib(name),
+ files := []string{
+ dexJarFileApexRootRelative,
+ }
+ if BoolDefault(d.importDexpreoptProperties.Dex_preopt.Profile_guided, false) {
+ files = append(files, dexJarFileApexRootRelative+".prof")
}
+ return files
}
// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
@@ -2157,7 +2201,7 @@ var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
name := j.BaseModuleName()
- return requiredFilesFromPrebuiltApexForImport(name)
+ return requiredFilesFromPrebuiltApexForImport(name, &j.dexpreopter)
}
// Add compile time check for interface implementation
@@ -2198,6 +2242,7 @@ func ImportFactory() android.Module {
module.AddProperties(
&module.properties,
&module.dexer.dexProperties,
+ &module.importDexpreoptProperties,
)
module.initModuleAndImport(module)
@@ -2562,10 +2607,10 @@ func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorConte
type javaCommonAttributes struct {
*javaResourcesAttributes
- Srcs bazel.LabelListAttribute
- Plugins bazel.LabelListAttribute
- Javacopts bazel.StringListAttribute
- Common_srcs bazel.LabelListAttribute
+ *kotlinAttributes
+ Srcs bazel.LabelListAttribute
+ Plugins bazel.LabelListAttribute
+ Javacopts bazel.StringListAttribute
}
type javaDependencyLabels struct {
@@ -2592,8 +2637,8 @@ type javaAidlLibraryAttributes struct {
// depending on the module type.
type bp2BuildJavaInfo struct {
// separates dependencies into dynamic dependencies and static dependencies.
- DepLabels *javaDependencyLabels
- hasKotlinSrcs bool
+ DepLabels *javaDependencyLabels
+ hasKotlin bool
}
// convertLibraryAttrsBp2Build returns a javaCommonAttributes struct with
@@ -2615,6 +2660,7 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
}
}
}
+ srcs.ResolveExcludes()
javaSrcPartition := "java"
protoSrcPartition := "proto"
@@ -2740,9 +2786,18 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
depLabels.Deps = deps
depLabels.StaticDeps = bazel.MakeLabelListAttribute(staticDeps)
+ hasKotlin := !kotlinSrcs.IsEmpty()
+ commonAttrs.kotlinAttributes = &kotlinAttributes{
+ Kotlincflags: &m.properties.Kotlincflags,
+ }
+ if len(m.properties.Common_srcs) != 0 {
+ hasKotlin = true
+ commonAttrs.kotlinAttributes.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs))
+ }
+
bp2BuildInfo := &bp2BuildJavaInfo{
- DepLabels: depLabels,
- hasKotlinSrcs: !kotlinSrcs.IsEmpty(),
+ DepLabels: depLabels,
+ hasKotlin: hasKotlin,
}
return commonAttrs, bp2BuildInfo
@@ -2755,6 +2810,25 @@ type javaLibraryAttributes struct {
Neverlink bazel.BoolAttribute
}
+type kotlinAttributes struct {
+ Common_srcs bazel.LabelListAttribute
+ Kotlincflags *[]string
+}
+
+func ktJvmLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties {
+ return bazel.BazelTargetModuleProperties{
+ Rule_class: "kt_jvm_library",
+ Bzl_load_location: "//build/bazel/rules/kotlin:rules.bzl",
+ }
+}
+
+func javaLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties {
+ return bazel.BazelTargetModuleProperties{
+ Rule_class: "java_library",
+ Bzl_load_location: "//build/bazel/rules/java:rules.bzl",
+ }
+}
+
func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
commonAttrs, bp2BuildInfo := m.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2BuildInfo.DepLabels
@@ -2767,6 +2841,9 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
if sdkVersion.Kind == android.SdkPublic && sdkVersion.ApiLevel == android.FutureApiLevel {
// TODO(b/220869005) remove forced dependency on current public android.jar
deps.Add(bazel.MakeLabelAttribute("//prebuilts/sdk:public_current_android_sdk_java_import"))
+ } else if sdkVersion.Kind == android.SdkSystem && sdkVersion.ApiLevel == android.FutureApiLevel {
+ // TODO(b/215230098) remove forced dependency on current public android.jar
+ deps.Add(bazel.MakeLabelAttribute("//prebuilts/sdk:system_current_android_sdk_java_import"))
}
} else if !deps.IsEmpty() {
ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.")
@@ -2780,18 +2857,10 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
}
name := m.Name()
- if !bp2BuildInfo.hasKotlinSrcs && len(m.properties.Common_srcs) == 0 {
- props = bazel.BazelTargetModuleProperties{
- Rule_class: "java_library",
- Bzl_load_location: "//build/bazel/rules/java:library.bzl",
- }
+ if !bp2BuildInfo.hasKotlin {
+ props = javaLibraryBazelTargetModuleProperties()
} else {
- attrs.javaCommonAttributes.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs))
-
- props = bazel.BazelTargetModuleProperties{
- Rule_class: "kt_jvm_library",
- Bzl_load_location: "@rules_kotlin//kotlin:jvm_library.bzl",
- }
+ props = ktJvmLibraryBazelTargetModuleProperties()
}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
@@ -2870,55 +2939,38 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
}
props := bazel.BazelTargetModuleProperties{
- Rule_class: "java_binary",
+ Rule_class: "java_binary",
+ Bzl_load_location: "//build/bazel/rules/java:rules.bzl",
}
- attrs := &javaBinaryHostAttributes{
+ binAttrs := &javaBinaryHostAttributes{
Runtime_deps: runtimeDeps,
Main_class: mainClass,
Jvm_flags: jvmFlags,
}
- if !bp2BuildInfo.hasKotlinSrcs && len(m.properties.Common_srcs) == 0 {
- attrs.javaCommonAttributes = commonAttrs
- attrs.Deps = deps
- } else {
- ktName := m.Name() + "_kt"
- ktProps := bazel.BazelTargetModuleProperties{
- Rule_class: "kt_jvm_library",
- Bzl_load_location: "@rules_kotlin//kotlin:jvm_library.bzl",
- }
- ktAttrs := &javaLibraryAttributes{
- Deps: deps,
- javaCommonAttributes: &javaCommonAttributes{
- Srcs: commonAttrs.Srcs,
- Plugins: commonAttrs.Plugins,
- Javacopts: commonAttrs.Javacopts,
- },
- }
-
- if len(m.properties.Common_srcs) != 0 {
- ktAttrs.javaCommonAttributes.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs))
- }
-
- // kt_jvm_library does not support resource_strip_prefix, if this attribute
- // is set, than javaResourcesAttributes needs to be set in the
- // javaCommonAttributes of the java_binary target
- if commonAttrs.javaResourcesAttributes != nil {
- if commonAttrs.javaResourcesAttributes.Resource_strip_prefix != nil {
- attrs.javaCommonAttributes = &javaCommonAttributes{
- javaResourcesAttributes: commonAttrs.javaResourcesAttributes,
- }
- } else {
- ktAttrs.javaCommonAttributes.javaResourcesAttributes = commonAttrs.javaResourcesAttributes
- }
- }
+ if commonAttrs.Srcs.IsEmpty() {
+ binAttrs.javaCommonAttributes = commonAttrs
+ ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs)
+ return
+ }
- ctx.CreateBazelTargetModule(ktProps, android.CommonAttributes{Name: ktName}, ktAttrs)
- attrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + ktName}})
+ libName := m.Name() + "_lib"
+ var libProps bazel.BazelTargetModuleProperties
+ if bp2BuildInfo.hasKotlin {
+ libProps = ktJvmLibraryBazelTargetModuleProperties()
+ } else {
+ libProps = javaLibraryBazelTargetModuleProperties()
+ }
+ libAttrs := &javaLibraryAttributes{
+ Deps: deps,
+ javaCommonAttributes: commonAttrs,
}
+ ctx.CreateBazelTargetModule(libProps, android.CommonAttributes{Name: libName}, libAttrs)
+ binAttrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}})
+
// Create the BazelTargetModule.
- ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
+ ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs)
}
type bazelJavaImportAttributes struct {
@@ -2942,7 +2994,10 @@ func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
attrs := &bazelJavaImportAttributes{
Jars: jars,
}
- props := bazel.BazelTargetModuleProperties{Rule_class: "java_import"}
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "java_import",
+ Bzl_load_location: "//build/bazel/rules/java:rules.bzl",
+ }
name := android.RemoveOptionalPrebuiltPrefix(i.Name())
@@ -2953,7 +3008,10 @@ func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Neverlink: bazel.BoolAttribute{Value: &neverlink},
Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}),
}
- ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{Rule_class: "java_library"}, android.CommonAttributes{Name: name + "-neverlink"}, neverlinkAttrs)
+ ctx.CreateBazelTargetModule(
+ javaLibraryBazelTargetModuleProperties(),
+ android.CommonAttributes{Name: name + "-neverlink"},
+ neverlinkAttrs)
}