diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 207 |
1 files changed, 154 insertions, 53 deletions
diff --git a/java/java.go b/java/java.go index e37a77e46..d85f3e16e 100644 --- a/java/java.go +++ b/java/java.go @@ -294,6 +294,11 @@ type UsesLibraryDependency interface { ClassLoaderContexts() dexpreopt.ClassLoaderContextMap } +// Provides transitive Proguard flag files to downstream DEX jars. +type LibraryDependency interface { + ExportedProguardFlagFiles() android.Paths +} + // TODO(jungjw): Move this to kythe.go once it's created. type xref interface { XrefJavaFiles() android.Paths @@ -516,6 +521,11 @@ func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext an } } +// Java version for stubs generation +func getStubsJavaVersion() javaVersion { + return JAVA_VERSION_8 +} + type javaVersion int const ( @@ -596,9 +606,17 @@ func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) jav type Library struct { Module + exportedProguardFlagFiles android.Paths + InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths) } +var _ LibraryDependency = (*Library)(nil) + +func (j *Library) ExportedProguardFlagFiles() android.Paths { + return j.exportedProguardFlagFiles +} + var _ android.ApexModule = (*Library)(nil) // Provides access to the list of permitted packages from apex boot jars. @@ -694,6 +712,15 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { } j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...) } + + j.exportedProguardFlagFiles = append(j.exportedProguardFlagFiles, + android.PathsForModuleSrc(ctx, j.dexProperties.Optimize.Proguard_flags_files)...) + ctx.VisitDirectDeps(func(m android.Module) { + if lib, ok := m.(LibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { + j.exportedProguardFlagFiles = append(j.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...) + } + }) + j.exportedProguardFlagFiles = android.FirstUniquePaths(j.exportedProguardFlagFiles) } func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) { @@ -849,7 +876,6 @@ func LibraryFactory() android.Module { module.initModuleAndImport(module) android.InitApexModule(module) - android.InitSdkAwareModule(module) android.InitBazelModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module @@ -872,7 +898,6 @@ func LibraryHostFactory() android.Module { module.Module.properties.Installable = proptools.BoolPtr(true) android.InitApexModule(module) - android.InitSdkAwareModule(module) android.InitBazelModule(module) InitJavaModule(module, android.HostSupported) return module @@ -888,6 +913,10 @@ type TestOptions struct { // a list of extra test configuration files that should be installed with the module. Extra_test_configs []string `android:"path,arch_variant"` + + // Extra <option> tags to add to the auto generated test xml file. The "key" + // is optional in each of these. + Tradefed_options []tradefed.Option } type testProperties struct { @@ -1165,9 +1194,18 @@ func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites) j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest) } - - j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, - j.testProperties.Test_suites, configs, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test) + j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ + TestConfigProp: j.testProperties.Test_config, + TestConfigTemplateProp: j.testProperties.Test_config_template, + TestSuites: j.testProperties.Test_suites, + Config: configs, + OptionsForAutogenerated: j.testProperties.Test_options.Tradefed_options, + AutoGenConfig: j.testProperties.Auto_gen_config, + UnitTest: j.testProperties.Test_options.Unit_test, + DeviceTemplate: "${JavaTestConfigTemplate}", + HostTemplate: "${JavaHostTestConfigTemplate}", + HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}", + }) j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data) @@ -1212,8 +1250,13 @@ func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContex } func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { - j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil, - j.prebuiltTestProperties.Test_suites, nil, nil, nil) + j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ + TestConfigProp: j.prebuiltTestProperties.Test_config, + TestSuites: j.prebuiltTestProperties.Test_suites, + DeviceTemplate: "${JavaTestConfigTemplate}", + HostTemplate: "${JavaHostTestConfigTemplate}", + HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}", + }) j.Import.GenerateAndroidBuildActions(ctx) } @@ -1295,7 +1338,6 @@ func TestFactory() android.Module { module.Module.dexpreopter.isTest = true module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) - android.InitSdkAwareModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module } @@ -1334,7 +1376,6 @@ func JavaTestImportFactory() android.Module { android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) - android.InitSdkAwareModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module } @@ -1567,17 +1608,27 @@ type JavaApiLibraryProperties struct { Api_surface *string // list of Java API contribution modules that consists this API surface + // This is a list of Soong modules Api_contributions []string + // list of api.txt files relative to this directory that contribute to the + // API surface. + // This is a list of relative paths + Api_files []string + // List of flags to be passed to the javac compiler to generate jar file Javacflags []string + + // List of shared java libs that this module has dependencies to and + // should be passed as classpath in javac invocation + Libs []string } func ApiLibraryFactory() android.Module { module := &ApiLibrary{} android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) - android.InitDefaultableModule(module) module.AddProperties(&module.properties) + android.InitDefaultableModule(module) return module } @@ -1642,6 +1693,7 @@ func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { for _, apiContributionName := range apiContributions { ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName) } + ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...) } func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { @@ -1659,12 +1711,27 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { homeDir := android.PathForModuleOut(ctx, "metalava", "home") - var srcFiles []android.Path - ctx.VisitDirectDepsWithTag(javaApiContributionTag, func(dep android.Module) { - provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo) - srcFiles = append(srcFiles, android.PathForSource(ctx, provider.ApiFile.String())) + var srcFiles android.Paths + var classPaths android.Paths + ctx.VisitDirectDeps(func(dep android.Module) { + tag := ctx.OtherModuleDependencyTag(dep) + switch tag { + case javaApiContributionTag: + provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo) + srcFiles = append(srcFiles, android.PathForSource(ctx, provider.ApiFile.String())) + case libTag: + provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) + classPaths = append(classPaths, provider.HeaderJars...) + } }) + // Add the api_files inputs + for _, api := range al.properties.Api_files { + // Use MaybeExistentPathForSource since the api file might not exist during analysis. + // This will be provided by the orchestrator in the combined execution. + srcFiles = append(srcFiles, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), api)) + } + cmd := metalavaStubCmd(ctx, rule, srcFiles, homeDir) al.stubsFlags(ctx, cmd, stubsDir) @@ -1683,12 +1750,18 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), "android.jar") var flags javaBuilderFlags + flags.javaVersion = getStubsJavaVersion() flags.javacFlags = strings.Join(al.properties.Javacflags, " ") + flags.classpath = classpath(classPaths) TransformJavaToClasses(ctx, al.stubsJar, 0, android.Paths{}, android.Paths{al.stubsSrcJar}, flags, android.Paths{}) ctx.Phony(ctx.ModuleName(), al.stubsJar) + + ctx.SetProvider(JavaInfoProvider, JavaInfo{ + HeaderJars: android.PathsIfNonNil(al.stubsJar), + }) } // @@ -1743,7 +1816,6 @@ type Import struct { android.ApexModuleBase android.BazelModuleBase prebuilt android.Prebuilt - android.SdkBase // Functionality common to Module and Import. embeddableInModuleAndImport @@ -2117,7 +2189,6 @@ func ImportFactory() android.Module { android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) - android.InitSdkAwareModule(module) android.InitBazelModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module @@ -2349,6 +2420,7 @@ func DefaultsFactory() android.Module { &RuntimeResourceOverlayProperties{}, &LintProperties{}, &appTestHelperAppProperties{}, + &JavaApiLibraryProperties{}, ) android.InitDefaultsModule(module) @@ -2473,9 +2545,10 @@ func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorConte type javaCommonAttributes struct { *javaResourcesAttributes - Srcs bazel.LabelListAttribute - Plugins bazel.LabelListAttribute - Javacopts bazel.StringListAttribute + Srcs bazel.LabelListAttribute + Plugins bazel.LabelListAttribute + Javacopts bazel.StringListAttribute + Common_srcs bazel.LabelListAttribute } type javaDependencyLabels struct { @@ -2620,17 +2693,10 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) } if m.properties.Libs != nil { - - // TODO 244210934 ALIX Check if this else statement breaks presubmits get rid of it if it doesn't - if strings.HasPrefix(ctx.ModuleType(), "java_binary") || strings.HasPrefix(ctx.ModuleType(), "java_library") { - for _, d := range m.properties.Libs { - neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) - neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" - deps.Add(&neverlinkLabel) - } - - } else { - deps.Append(android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(android.CopyOf(m.properties.Libs)))) + for _, d := range m.properties.Libs { + neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) + neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" + deps.Add(&neverlinkLabel) } } @@ -2661,10 +2727,9 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) type javaLibraryAttributes struct { *javaCommonAttributes - Deps bazel.LabelListAttribute - Exports bazel.LabelListAttribute - Neverlink bazel.BoolAttribute - Common_srcs bazel.LabelListAttribute + Deps bazel.LabelListAttribute + Exports bazel.LabelListAttribute + Neverlink bazel.BoolAttribute } func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { @@ -2697,25 +2762,23 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { Rule_class: "java_library", Bzl_load_location: "//build/bazel/rules/java:library.bzl", } - - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) - neverlinkProp := true - neverLinkAttrs := &javaLibraryAttributes{ - Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), - Neverlink: bazel.BoolAttribute{Value: &neverlinkProp}, - } - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name + "-neverlink"}, neverLinkAttrs) } else { - attrs.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs)) + 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", } - // TODO (b/244210934): create neverlink-duplicate target once kt_jvm_library supports neverlink attribute - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) } + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) + neverlinkProp := true + neverLinkAttrs := &javaLibraryAttributes{ + Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), + Neverlink: bazel.BoolAttribute{Value: &neverlinkProp}, + } + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name + "-neverlink"}, neverLinkAttrs) + } type javaBinaryHostAttributes struct { @@ -2756,14 +2819,8 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) { mainClass = mainClassInManifest } - attrs := &javaBinaryHostAttributes{ - javaCommonAttributes: commonAttrs, - Deps: deps, - Runtime_deps: runtimeDeps, - Main_class: mainClass, - } - // Attribute jvm_flags + var jvmFlags bazel.StringListAttribute if m.binaryProperties.Jni_libs != nil { jniLibPackages := map[string]bool{} for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes { @@ -2786,12 +2843,56 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) { // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage) } - attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")}) + jvmFlags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")}) } props := bazel.BazelTargetModuleProperties{ Rule_class: "java_binary", } + attrs := &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 + } + } + + ctx.CreateBazelTargetModule(ktProps, android.CommonAttributes{Name: ktName}, ktAttrs) + attrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + ktName}}) + } // Create the BazelTargetModule. ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |