summaryrefslogtreecommitdiff
path: root/java/java.go
diff options
context:
space:
mode:
Diffstat (limited to 'java/java.go')
-rw-r--r--java/java.go145
1 files changed, 89 insertions, 56 deletions
diff --git a/java/java.go b/java/java.go
index 521aef301..bf692be24 100644
--- a/java/java.go
+++ b/java/java.go
@@ -64,6 +64,7 @@ func registerJavaBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("dex_import", DexImportFactory)
ctx.RegisterModuleType("java_api_library", ApiLibraryFactory)
ctx.RegisterModuleType("java_api_contribution", ApiContributionFactory)
+ ctx.RegisterModuleType("java_api_contribution_import", ApiContributionImportFactory)
// This mutator registers dependencies on dex2oat for modules that should be
// dexpreopted. This is done late when the final variants have been
@@ -1623,7 +1624,8 @@ func ApiContributionFactory() android.Module {
}
type JavaApiImportInfo struct {
- ApiFile android.Path
+ ApiFile android.Path
+ ApiSurface string
}
var JavaApiImportProvider = blueprint.NewProvider(JavaApiImportInfo{})
@@ -1635,7 +1637,8 @@ func (ap *JavaApiContribution) GenerateAndroidBuildActions(ctx android.ModuleCon
}
ctx.SetProvider(JavaApiImportProvider, JavaApiImportInfo{
- ApiFile: apiFile,
+ ApiFile: apiFile,
+ ApiSurface: proptools.String(ap.properties.Api_surface),
})
}
@@ -1664,11 +1667,6 @@ type JavaApiLibraryProperties struct {
// 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 `android:"path"`
-
// List of flags to be passed to the javac compiler to generate jar file
Javacflags []string
@@ -1798,6 +1796,7 @@ func (al *ApiLibrary) extractApiSrcs(ctx android.ModuleContext, rule *android.Ru
Flag("-jar").
Flag("-write_if_changed").
Flag("-ignore_missing_files").
+ Flag("-quiet").
FlagWithArg("-C ", unzippedSrcJarDir.String()).
FlagWithInput("-l ", classFilesList).
FlagWithOutput("-o ", al.stubsJarWithoutStaticLibs)
@@ -1815,26 +1814,22 @@ func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
}
}
-// API signature file names sorted from
-// the narrowest api scope to the widest api scope
-var scopeOrderedSourceFileNames = allApiScopes.Strings(
- func(s *apiScope) string { return s.apiFilePrefix + "current.txt" })
-
-func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFiles android.Paths) android.Paths {
- sortedSrcFiles := android.Paths{}
+// Map where key is the api scope name and value is the int value
+// representing the order of the api scope, narrowest to the widest
+var scopeOrderMap = allApiScopes.MapToIndex(
+ func(s *apiScope) string { return s.name })
- for _, scopeSourceFileName := range scopeOrderedSourceFileNames {
- for _, sourceFileName := range srcFiles {
- if sourceFileName.Base() == scopeSourceFileName {
- sortedSrcFiles = append(sortedSrcFiles, sourceFileName)
- }
+func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFilesInfo []JavaApiImportInfo) []JavaApiImportInfo {
+ for _, srcFileInfo := range srcFilesInfo {
+ if srcFileInfo.ApiSurface == "" {
+ ctx.ModuleErrorf("Api surface not defined for the associated api file %s", srcFileInfo.ApiFile)
}
}
- if len(srcFiles) != len(sortedSrcFiles) {
- ctx.ModuleErrorf("Unrecognizable source file found within %s", srcFiles)
- }
+ sort.Slice(srcFilesInfo, func(i, j int) bool {
+ return scopeOrderMap[srcFilesInfo[i].ApiSurface] < scopeOrderMap[srcFilesInfo[j].ApiSurface]
+ })
- return sortedSrcFiles
+ return srcFilesInfo
}
func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -1852,7 +1847,7 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
homeDir := android.PathForModuleOut(ctx, "metalava", "home")
- var srcFiles android.Paths
+ var srcFilesInfo []JavaApiImportInfo
var classPaths android.Paths
var staticLibs android.Paths
var depApiSrcsStubsJar android.Path
@@ -1861,11 +1856,10 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
switch tag {
case javaApiContributionTag:
provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo)
- providerApiFile := provider.ApiFile
- if providerApiFile == nil && !ctx.Config().AllowMissingDependencies() {
+ if provider.ApiFile == nil && !ctx.Config().AllowMissingDependencies() {
ctx.ModuleErrorf("Error: %s has an empty api file.", dep.Name())
}
- srcFiles = append(srcFiles, android.PathForSource(ctx, providerApiFile.String()))
+ srcFilesInfo = append(srcFilesInfo, provider)
case libTag:
provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
classPaths = append(classPaths, provider.HeaderJars...)
@@ -1878,17 +1872,16 @@ func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
}
})
- // Add the api_files inputs
- for _, api := range al.properties.Api_files {
- srcFiles = append(srcFiles, android.PathForModuleSrc(ctx, api))
+ srcFilesInfo = al.sortApiFilesByApiScope(ctx, srcFilesInfo)
+ var srcFiles android.Paths
+ for _, srcFileInfo := range srcFilesInfo {
+ srcFiles = append(srcFiles, android.PathForSource(ctx, srcFileInfo.ApiFile.String()))
}
if srcFiles == nil && !ctx.Config().AllowMissingDependencies() {
ctx.ModuleErrorf("Error: %s has an empty api file.", ctx.ModuleName())
}
- srcFiles = al.sortApiFilesByApiScope(ctx, srcFiles)
-
cmd := metalavaStubCmd(ctx, rule, srcFiles, homeDir)
al.stubsFlags(ctx, cmd, stubsDir)
@@ -2758,7 +2751,7 @@ type javaResourcesAttributes struct {
Additional_resources bazel.LabelListAttribute
}
-func (m *Library) getResourceFilegroupStripPrefix(ctx android.TopDownMutatorContext, resourceFilegroup string) (*string, bool) {
+func (m *Library) getResourceFilegroupStripPrefix(ctx android.Bp2buildMutatorContext, resourceFilegroup string) (*string, bool) {
if otherM, ok := ctx.ModuleFromName(resourceFilegroup); ok {
if fg, isFilegroup := otherM.(android.FileGroupPath); isFilegroup {
return proptools.StringPtr(filepath.Join(ctx.OtherModuleDir(otherM), fg.GetPath(ctx))), true
@@ -2767,7 +2760,7 @@ func (m *Library) getResourceFilegroupStripPrefix(ctx android.TopDownMutatorCont
return proptools.StringPtr(""), false
}
-func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes {
+func (m *Library) convertJavaResourcesAttributes(ctx android.Bp2buildMutatorContext) *javaResourcesAttributes {
var resources bazel.LabelList
var resourceStripPrefix *string
@@ -2826,7 +2819,7 @@ func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorConte
if resourceStripPrefix == nil && i == 0 {
resourceStripPrefix = resAttr.Resource_strip_prefix
resources = resAttr.Resources.Value
- } else {
+ } else if !resAttr.Resources.IsEmpty() {
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{
Rule_class: "java_resources",
@@ -2898,14 +2891,19 @@ func javaXsdTargetName(xsd android.XsdConfigBp2buildTargets) string {
// which has other non-attribute information needed for bp2build conversion
// that needs different handling depending on the module types, and thus needs
// to be returned to the calling function.
-func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *bp2BuildJavaInfo, bool) {
+func (m *Library) convertLibraryAttrsBp2Build(ctx android.Bp2buildMutatorContext) (*javaCommonAttributes, *bp2BuildJavaInfo, bool) {
var srcs bazel.LabelListAttribute
var deps bazel.LabelListAttribute
var staticDeps bazel.LabelListAttribute
if proptools.String(m.deviceProperties.Sdk_version) == "" && m.DeviceSupported() {
+ // TODO(b/297356704): handle platform apis in bp2build
ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "sdk_version unset")
return &javaCommonAttributes{}, &bp2BuildJavaInfo{}, false
+ } else if proptools.String(m.deviceProperties.Sdk_version) == "core_platform" {
+ // TODO(b/297356582): handle core_platform in bp2build
+ ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "sdk_version core_platform")
+ return &javaCommonAttributes{}, &bp2BuildJavaInfo{}, false
}
archVariantProps := m.GetArchVariantProperties(ctx, &CommonProperties{})
@@ -3045,17 +3043,6 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
}
}
- protoDepLabel := bp2buildProto(ctx, &m.Module, srcPartitions[protoSrcPartition])
- // Soong does not differentiate between a java_library and the Bazel equivalent of
- // a java_proto_library + proto_library pair. Instead, in Soong proto sources are
- // listed directly in the srcs of a java_library, and the classes produced
- // by protoc are included directly in the resulting JAR. Thus upstream dependencies
- // that depend on a java_library with proto sources can link directly to the protobuf API,
- // and so this should be a static dependency.
- if protoDepLabel != nil {
- staticDeps.Append(bazel.MakeSingleLabelListAttribute(*protoDepLabel))
- }
-
depLabels := &javaDependencyLabels{}
depLabels.Deps = deps
@@ -3071,6 +3058,20 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
}
depLabels.StaticDeps.Append(staticDeps)
+ var additionalProtoDeps bazel.LabelListAttribute
+ additionalProtoDeps.Append(depLabels.Deps)
+ additionalProtoDeps.Append(depLabels.StaticDeps)
+ protoDepLabel := bp2buildProto(ctx, &m.Module, srcPartitions[protoSrcPartition], additionalProtoDeps)
+ // Soong does not differentiate between a java_library and the Bazel equivalent of
+ // a java_proto_library + proto_library pair. Instead, in Soong proto sources are
+ // listed directly in the srcs of a java_library, and the classes produced
+ // by protoc are included directly in the resulting JAR. Thus upstream dependencies
+ // that depend on a java_library with proto sources can link directly to the protobuf API,
+ // and so this should be a static dependency.
+ if protoDepLabel != nil {
+ depLabels.StaticDeps.Append(bazel.MakeSingleLabelListAttribute(*protoDepLabel))
+ }
+
hasKotlin := !kotlinSrcs.IsEmpty()
commonAttrs.kotlinAttributes = &kotlinAttributes{
Kotlincflags: &m.properties.Kotlincflags,
@@ -3114,7 +3115,7 @@ func javaLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties
}
}
-func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
+func javaLibraryBp2Build(ctx android.Bp2buildMutatorContext, m *Library) {
commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx)
if !supported {
return
@@ -3122,16 +3123,22 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps
+ exports := depLabels.StaticDeps
if !commonAttrs.Srcs.IsEmpty() {
- deps.Append(depLabels.StaticDeps) // we should only append these if there are sources to use them
+ deps.Append(exports) // we should only append these if there are sources to use them
} else if !deps.IsEmpty() {
- ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.")
+ // java_library does not accept deps when there are no srcs because
+ // there is no compilation happening, but it accepts exports.
+ // The non-empty deps here are unnecessary as deps on the java_library
+ // since they aren't being propagated to any dependencies.
+ // So we can drop deps here.
+ deps = bazel.LabelListAttribute{}
}
var props bazel.BazelTargetModuleProperties
attrs := &javaLibraryAttributes{
javaCommonAttributes: commonAttrs,
Deps: deps,
- Exports: depLabels.StaticDeps,
+ Exports: exports,
}
name := m.Name()
@@ -3164,7 +3171,7 @@ type javaBinaryHostAttributes struct {
}
// JavaBinaryHostBp2Build is for java_binary_host bp2build.
-func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
+func javaBinaryHostBp2Build(ctx android.Bp2buildMutatorContext, m *Binary) {
commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx)
if !supported {
return
@@ -3251,7 +3258,7 @@ type javaTestHostAttributes struct {
}
// javaTestHostBp2Build is for java_test_host bp2build.
-func javaTestHostBp2Build(ctx android.TopDownMutatorContext, m *TestHost) {
+func javaTestHostBp2Build(ctx android.Bp2buildMutatorContext, m *TestHost) {
commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx)
if !supported {
return
@@ -3304,7 +3311,7 @@ type libraryCreationInfo struct {
// helper function that creates java_library target from java_binary_host or java_test_host,
// and returns the library target name,
-func createLibraryTarget(ctx android.TopDownMutatorContext, libInfo libraryCreationInfo) string {
+func createLibraryTarget(ctx android.Bp2buildMutatorContext, libInfo libraryCreationInfo) string {
libName := libInfo.baseName + "_lib"
var libProps bazel.BazelTargetModuleProperties
if libInfo.hasKotlin {
@@ -3327,7 +3334,7 @@ type bazelJavaImportAttributes struct {
}
// java_import bp2Build converter.
-func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
+func (i *Import) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
var jars bazel.LabelListAttribute
archVariantProps := i.GetArchVariantProperties(ctx, &ImportProperties{})
for axis, configToProps := range archVariantProps {
@@ -3363,7 +3370,6 @@ func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
javaLibraryBazelTargetModuleProperties(),
android.CommonAttributes{Name: name + "-neverlink"},
neverlinkAttrs)
-
}
var _ android.MixedBuildBuildable = (*Import)(nil)
@@ -3416,3 +3422,30 @@ func (i *Import) QueueBazelCall(ctx android.BaseModuleContext) {
func (i *Import) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
return true
}
+
+type JavaApiContributionImport struct {
+ JavaApiContribution
+
+ prebuilt android.Prebuilt
+}
+
+func ApiContributionImportFactory() android.Module {
+ module := &JavaApiContributionImport{}
+ android.InitAndroidModule(module)
+ android.InitDefaultableModule(module)
+ android.InitPrebuiltModule(module, &[]string{""})
+ module.AddProperties(&module.properties)
+ return module
+}
+
+func (module *JavaApiContributionImport) Prebuilt() *android.Prebuilt {
+ return &module.prebuilt
+}
+
+func (module *JavaApiContributionImport) Name() string {
+ return module.prebuilt.Name(module.ModuleBase.Name())
+}
+
+func (ap *JavaApiContributionImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+ ap.JavaApiContribution.GenerateAndroidBuildActions(ctx)
+}