diff options
Diffstat (limited to 'java/java.go')
-rw-r--r-- | java/java.go | 197 |
1 files changed, 140 insertions, 57 deletions
diff --git a/java/java.go b/java/java.go index 275abbe35..659f98a7c 100644 --- a/java/java.go +++ b/java/java.go @@ -230,6 +230,12 @@ type JavaInfo struct { // against this module. If empty, ImplementationJars should be used instead. HeaderJars android.Paths + // set of header jars for all transitive libs deps + TransitiveLibsHeaderJars *android.DepSet + + // set of header jars for all transitive static libs deps + TransitiveStaticLibsHeaderJars *android.DepSet + // ImplementationAndResourceJars is a list of jars that contain the implementations of classes // in the module as well as any resources included in the module. ImplementationAndResourcesJars android.Paths @@ -380,6 +386,7 @@ var ( instrumentationForTag = dependencyTag{name: "instrumentation_for"} extraLintCheckTag = dependencyTag{name: "extra-lint-check", toolchain: true} jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true} + r8LibraryJarTag = dependencyTag{name: "r8-libraryjar", runtimeLinked: true} syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"} jniInstallTag = installDependencyTag{name: "jni install"} binaryInstallTag = installDependencyTag{name: "binary install"} @@ -876,7 +883,6 @@ func LibraryFactory() android.Module { module.initModuleAndImport(module) android.InitApexModule(module) - android.InitSdkAwareModule(module) android.InitBazelModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module @@ -899,7 +905,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 @@ -915,6 +920,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 { @@ -1192,9 +1201,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) @@ -1239,8 +1257,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) } @@ -1322,7 +1345,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 } @@ -1361,7 +1383,6 @@ func JavaTestImportFactory() android.Module { android.InitPrebuiltModule(module, &module.properties.Jars) android.InitApexModule(module) - android.InitSdkAwareModule(module) InitJavaModule(module, android.HostAndDeviceSupported) return module } @@ -1573,7 +1594,11 @@ type JavaApiImportInfo struct { var JavaApiImportProvider = blueprint.NewProvider(JavaApiImportInfo{}) func (ap *JavaApiContribution) GenerateAndroidBuildActions(ctx android.ModuleContext) { - apiFile := android.PathForModuleSrc(ctx, String(ap.properties.Api_file)) + var apiFile android.Path = nil + if apiFileString := ap.properties.Api_file; apiFileString != nil { + apiFile = android.PathForModuleSrc(ctx, String(apiFileString)) + } + ctx.SetProvider(JavaApiImportProvider, JavaApiImportInfo{ ApiFile: apiFile, }) @@ -1604,13 +1629,17 @@ type JavaApiLibraryProperties struct { // 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 } @@ -1675,6 +1704,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) { @@ -1692,10 +1722,22 @@ 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) + providerApiFile := provider.ApiFile + if providerApiFile == nil { + ctx.ModuleErrorf("Error: %s has an empty api file.", dep.Name()) + } + srcFiles = append(srcFiles, android.PathForSource(ctx, providerApiFile.String())) + case libTag: + provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) + classPaths = append(classPaths, provider.HeaderJars...) + } }) // Add the api_files inputs @@ -1725,11 +1767,16 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { 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), + }) } // @@ -1784,7 +1831,6 @@ type Import struct { android.ApexModuleBase android.BazelModuleBase prebuilt android.Prebuilt - android.SdkBase // Functionality common to Module and Import. embeddableInModuleAndImport @@ -1913,9 +1959,9 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { var flags javaBuilderFlags + j.collectTransitiveHeaderJars(ctx) ctx.VisitDirectDeps(func(module android.Module) { tag := ctx.OtherModuleDependencyTag(module) - if ctx.OtherModuleHasProvider(module, JavaInfoProvider) { dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo) switch tag { @@ -2005,6 +2051,8 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { ctx.SetProvider(JavaInfoProvider, JavaInfo{ HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile), + TransitiveLibsHeaderJars: j.transitiveLibsHeaderJars, + TransitiveStaticLibsHeaderJars: j.transitiveStaticLibsHeaderJars, ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile), ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile), AidlIncludeDirs: j.exportAidlIncludeDirs, @@ -2158,7 +2206,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 @@ -2390,6 +2437,7 @@ func DefaultsFactory() android.Module { &RuntimeResourceOverlayProperties{}, &LintProperties{}, &appTestHelperAppProperties{}, + &JavaApiLibraryProperties{}, ) android.InitDefaultsModule(module) @@ -2514,9 +2562,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 { @@ -2554,7 +2603,7 @@ type bp2BuildJavaInfo struct { // to be returned to the calling function. func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *bp2BuildJavaInfo) { var srcs bazel.LabelListAttribute - var deps bazel.LabelList + var deps bazel.LabelListAttribute var staticDeps bazel.LabelList archVariantProps := m.GetArchVariantProperties(ctx, &CommonProperties{}) @@ -2660,18 +2709,17 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) Javacopts: bazel.MakeStringListAttribute(javacopts), } - 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) + for axis, configToProps := range archVariantProps { + for config, _props := range configToProps { + if archProps, ok := _props.(*CommonProperties); ok { + var libLabels []bazel.Label + for _, d := range archProps.Libs { + neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) + neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" + libLabels = append(libLabels, neverlinkLabel) + } + deps.SetSelectValue(axis, config, bazel.MakeLabelList(libLabels)) } - - } else { - deps.Append(android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(android.CopyOf(m.properties.Libs)))) } } @@ -2689,7 +2737,7 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) staticDeps.Add(protoDepLabel) depLabels := &javaDependencyLabels{} - depLabels.Deps = bazel.MakeLabelListAttribute(deps) + depLabels.Deps = deps depLabels.StaticDeps = bazel.MakeLabelListAttribute(staticDeps) bp2BuildInfo := &bp2BuildJavaInfo{ @@ -2702,10 +2750,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) { @@ -2738,25 +2785,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 { @@ -2797,14 +2842,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 { @@ -2827,12 +2866,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) |