Merge "Add the initial implementation of bp2build converter for java_library, java_library_host, java_binary_host and cc_library_host_shared so signapk tool can be built with Bazel." am: 2115d35101 am: 9adc5f84bb am: 38387e7624 am: b980aa5f6e

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1918122

Change-Id: If5a5f060862e1d6bc8ff2419c06d1d8a2e31d4ef
This commit is contained in:
Wei Li
2021-12-15 04:47:38 +00:00
committed by Automerger Merge Worker
8 changed files with 334 additions and 7 deletions

View File

@@ -372,6 +372,7 @@ type Module struct {
android.DefaultableModuleBase
android.ApexModuleBase
android.SdkBase
android.BazelModuleBase
// Functionality common to Module and Import.
embeddableInModuleAndImport
@@ -1952,3 +1953,17 @@ type ModuleWithStem interface {
}
var _ ModuleWithStem = (*Module)(nil)
func (j *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
switch ctx.ModuleType() {
case "java_library", "java_library_host":
if lib, ok := ctx.Module().(*Library); ok {
javaLibraryBp2Build(ctx, lib)
}
case "java_binary_host":
if binary, ok := ctx.Module().(*Binary); ok {
javaBinaryHostBp2Build(ctx, binary)
}
}
}

View File

@@ -21,7 +21,9 @@ package java
import (
"fmt"
"path/filepath"
"strings"
"android/soong/bazel"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -756,6 +758,7 @@ func LibraryFactory() android.Module {
android.InitApexModule(module)
android.InitSdkAwareModule(module)
android.InitBazelModule(module)
InitJavaModule(module, android.HostAndDeviceSupported)
return module
}
@@ -778,6 +781,7 @@ func LibraryHostFactory() android.Module {
android.InitApexModule(module)
android.InitSdkAwareModule(module)
android.InitBazelModule(module)
InitJavaModule(module, android.HostSupported)
return module
}
@@ -1228,6 +1232,8 @@ func BinaryFactory() android.Module {
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
android.InitDefaultableModule(module)
android.InitBazelModule(module)
return module
}
@@ -1245,6 +1251,7 @@ func BinaryHostFactory() android.Module {
android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
android.InitDefaultableModule(module)
android.InitBazelModule(module)
return module
}
@@ -1961,3 +1968,103 @@ func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
}
}
type javaLibraryAttributes struct {
Srcs bazel.LabelListAttribute
Deps bazel.LabelListAttribute
Javacopts bazel.StringListAttribute
}
func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
attrs := &javaLibraryAttributes{
Srcs: srcs,
}
if m.properties.Javacflags != nil {
attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
}
if m.properties.Libs != nil {
attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Libs))
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "java_library",
Bzl_load_location: "//build/bazel/rules/java:library.bzl",
}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
}
type javaBinaryHostAttributes struct {
Srcs bazel.LabelListAttribute
Deps bazel.LabelListAttribute
Main_class string
Jvm_flags bazel.StringListAttribute
}
// JavaBinaryHostBp2Build is for java_binary_host bp2build.
func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
mainClass := ""
if m.binaryProperties.Main_class != nil {
mainClass = *m.binaryProperties.Main_class
}
if m.properties.Manifest != nil {
mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String())
if err != nil {
return
}
mainClass = mainClassInManifest
}
srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
attrs := &javaBinaryHostAttributes{
Srcs: srcs,
Main_class: mainClass,
}
// Attribute deps
deps := []string{}
if m.properties.Static_libs != nil {
deps = append(deps, m.properties.Static_libs...)
}
if m.binaryProperties.Jni_libs != nil {
deps = append(deps, m.binaryProperties.Jni_libs...)
}
if len(deps) > 0 {
attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, deps))
}
// Attribute jvm_flags
if m.binaryProperties.Jni_libs != nil {
jniLibPackages := map[string]bool{}
for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes {
jniLibPackage := jniLibLabel.Label
indexOfColon := strings.Index(jniLibLabel.Label, ":")
if indexOfColon > 0 {
// JNI lib from other package
jniLibPackage = jniLibLabel.Label[2:indexOfColon]
} else if indexOfColon == 0 {
// JNI lib in the same package of java_binary
packageOfCurrentModule := m.GetBazelLabel(ctx, m)
jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")]
}
if _, inMap := jniLibPackages[jniLibPackage]; !inMap {
jniLibPackages[jniLibPackage] = true
}
}
jniLibPaths := []string{}
for jniLibPackage, _ := range jniLibPackages {
// 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, ":")})
}
props := bazel.BazelTargetModuleProperties{
Rule_class: "java_binary",
}
// Create the BazelTargetModule.
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
}