diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 114 |
1 files changed, 82 insertions, 32 deletions
diff --git a/java/java.go b/java/java.go index 10d60f096..7078cc38f 100644 --- a/java/java.go +++ b/java/java.go @@ -1625,13 +1625,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 } @@ -1696,6 +1700,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) { @@ -1713,10 +1718,18 @@ 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 @@ -1746,11 +1759,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), + }) } // @@ -2411,6 +2429,7 @@ func DefaultsFactory() android.Module { &RuntimeResourceOverlayProperties{}, &LintProperties{}, &appTestHelperAppProperties{}, + &JavaApiLibraryProperties{}, ) android.InitDefaultsModule(module) @@ -2535,9 +2554,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 { @@ -2682,17 +2702,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") || ctx.ModuleType() == "android_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) } } @@ -2723,10 +2736,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) { @@ -2760,7 +2772,7 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { Bzl_load_location: "//build/bazel/rules/java:library.bzl", } } 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", @@ -2816,14 +2828,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 { @@ -2846,12 +2852,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) |