add java_import to mixed build

Bug: 220168131
Test: go test ./java -run TestImportMixedBuild
Test: USE_BAZEL_ANALYSIS=1 m CtsManagedProfileApp && verify jars are
  included from execroot/__main__ directory
Test: build/bazel/ci/mixed_droid.sh
Change-Id: I6d35a2389ea35525d532efc8474c71d2c8825646
This commit is contained in:
Sam Delmerico
2022-02-25 17:04:37 +00:00
parent 6f84b7763e
commit 277795cb4f
3 changed files with 156 additions and 36 deletions

View File

@@ -24,6 +24,7 @@ import (
"strings"
"android/soong/bazel"
"android/soong/bazel/cquery"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -1610,7 +1611,8 @@ func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
}
}
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
func (j *Import) commonBuildActions(ctx android.ModuleContext) {
//TODO(b/231322772) these should come from Bazel once available
j.sdkVersion = j.SdkVersion(ctx)
j.minSdkVersion = j.MinSdkVersion(ctx)
@@ -1621,6 +1623,10 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
if ctx.Windows() {
j.HideFromMake()
}
}
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
j.commonBuildActions(ctx)
jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
@@ -1662,19 +1668,7 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
addCLCFromDep(ctx, module, j.classLoaderContexts)
})
if Bool(j.properties.Installable) {
var installDir android.InstallPath
if ctx.InstallInTestcases() {
var archDir string
if !ctx.Host() {
archDir = ctx.DeviceConfig().DeviceArch()
}
installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
} else {
installDir = android.PathForModuleInstall(ctx, "framework")
}
ctx.InstallFile(installDir, jarName, outputFile)
}
j.maybeInstall(ctx, jarName, outputFile)
j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
@@ -1748,6 +1742,24 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
})
}
func (j *Import) maybeInstall(ctx android.ModuleContext, jarName string, outputFile android.Path) {
if !Bool(j.properties.Installable) {
return
}
var installDir android.InstallPath
if ctx.InstallInTestcases() {
var archDir string
if !ctx.Host() {
archDir = ctx.DeviceConfig().DeviceArch()
}
installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
} else {
installDir = android.PathForModuleInstall(ctx, "framework")
}
ctx.InstallFile(installDir, jarName, outputFile)
}
func (j *Import) OutputFiles(tag string) (android.Paths, error) {
switch tag {
case "", ".jar":
@@ -2046,9 +2058,7 @@ func DexImportFactory() android.Module {
return module
}
//
// Defaults
//
type Defaults struct {
android.ModuleBase
android.DefaultsModuleBase
@@ -2063,29 +2073,29 @@ type Defaults struct {
//
// Example:
//
// java_defaults {
// name: "example_defaults",
// srcs: ["common/**/*.java"],
// javacflags: ["-Xlint:all"],
// aaptflags: ["--auto-add-overlay"],
// }
// java_defaults {
// name: "example_defaults",
// srcs: ["common/**/*.java"],
// javacflags: ["-Xlint:all"],
// aaptflags: ["--auto-add-overlay"],
// }
//
// java_library {
// name: "example",
// defaults: ["example_defaults"],
// srcs: ["example/**/*.java"],
// }
// java_library {
// name: "example",
// defaults: ["example_defaults"],
// srcs: ["example/**/*.java"],
// }
//
// is functionally identical to:
//
// java_library {
// name: "example",
// srcs: [
// "common/**/*.java",
// "example/**/*.java",
// ],
// javacflags: ["-Xlint:all"],
// }
// java_library {
// name: "example",
// srcs: [
// "common/**/*.java",
// "example/**/*.java",
// ],
// javacflags: ["-Xlint:all"],
// }
func DefaultsFactory() android.Module {
module := &Defaults{}
@@ -2483,5 +2493,54 @@ func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
props := bazel.BazelTargetModuleProperties{Rule_class: "java_import"}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: android.RemoveOptionalPrebuiltPrefix(i.Name())}, attrs)
}
var _ android.MixedBuildBuildable = (*Import)(nil)
func (i *Import) getBazelModuleLabel(ctx android.BaseModuleContext) string {
return android.RemoveOptionalPrebuiltPrefixFromBazelLabel(i.GetBazelLabel(ctx, i))
}
func (i *Import) ProcessBazelQueryResponse(ctx android.ModuleContext) {
i.commonBuildActions(ctx)
bazelCtx := ctx.Config().BazelContext
filePaths, err := bazelCtx.GetOutputFiles(i.getBazelModuleLabel(ctx), android.GetConfigKey(ctx))
if err != nil {
ctx.ModuleErrorf(err.Error())
return
}
bazelJars := android.Paths{}
for _, bazelOutputFile := range filePaths {
bazelJars = append(bazelJars, android.PathForBazelOut(ctx, bazelOutputFile))
}
jarName := android.RemoveOptionalPrebuiltPrefix(i.Name()) + ".jar"
outputFile := android.PathForModuleOut(ctx, "bazelCombined", jarName)
TransformJarsToJar(ctx, outputFile, "combine prebuilt jars", bazelJars,
android.OptionalPath{}, // manifest
false, // stripDirEntries
[]string{}, // filesToStrip
[]string{}, // dirsToStrip
)
i.combinedClasspathFile = outputFile
ctx.SetProvider(JavaInfoProvider, JavaInfo{
HeaderJars: android.PathsIfNonNil(i.combinedClasspathFile),
ImplementationAndResourcesJars: android.PathsIfNonNil(i.combinedClasspathFile),
ImplementationJars: android.PathsIfNonNil(i.combinedClasspathFile),
//TODO(b/240308299) include AIDL information from Bazel
})
i.maybeInstall(ctx, jarName, outputFile)
}
func (i *Import) QueueBazelCall(ctx android.BaseModuleContext) {
bazelCtx := ctx.Config().BazelContext
bazelCtx.QueueBazelRequest(i.getBazelModuleLabel(ctx), cquery.GetOutputFiles, android.GetConfigKey(ctx))
}
func (i *Import) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
return true
}