Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ed2491cf authored by Alix Espino's avatar Alix Espino Committed by Gerrit Code Review
Browse files

Merge "java_binary with kotlin sources"

parents 7def86db 7c8eaebc
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -669,6 +669,10 @@ var (
		// kotlin srcs in java libs
		"CtsPkgInstallerConstants",
		"kotlinx_atomicfu",

		// kotlin srcs in java binary
		"AnalyzerKt",
		"trebuchet-core",
	}

	Bp2buildModuleTypeAlwaysConvertList = []string{
+150 −0
Original line number Diff line number Diff line
@@ -134,3 +134,153 @@ java_import_host{
		},
	})
}

func TestJavaBinaryHostKotlinSrcs(t *testing.T) {
	runJavaBinaryHostTestCase(t, Bp2buildTestCase{
		Description: "java_binary_host with srcs, libs.",
		Filesystem:  testFs,
		Blueprint: `java_binary_host {
    name: "java-binary-host",
    manifest: "test.mf",
    srcs: ["a.java", "b.kt"],
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
				"srcs": `[
        "a.java",
        "b.kt",
    ]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
			MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
				"main_class":   `"com.android.test.MainClass"`,
				"runtime_deps": `[":java-binary-host_kt"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
}

func TestJavaBinaryHostKotlinCommonSrcs(t *testing.T) {
	runJavaBinaryHostTestCase(t, Bp2buildTestCase{
		Description: "java_binary_host with common_srcs",
		Filesystem:  testFs,
		Blueprint: `java_binary_host {
    name: "java-binary-host",
    manifest: "test.mf",
    srcs: ["a.java"],
    common_srcs: ["b.kt"],
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
				"srcs":        `["a.java"]`,
				"common_srcs": `["b.kt"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
			MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
				"main_class":   `"com.android.test.MainClass"`,
				"runtime_deps": `[":java-binary-host_kt"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
}

func TestJavaBinaryHostKotlinWithResourceDir(t *testing.T) {
	runJavaBinaryHostTestCase(t, Bp2buildTestCase{
		Description: "java_binary_host with srcs, libs, resource dir  .",
		Filesystem: map[string]string{
			"test.mf":        "Main-Class: com.android.test.MainClass",
			"res/a.res":      "",
			"res/dir1/b.res": "",
		},
		Blueprint: `java_binary_host {
    name: "java-binary-host",
    manifest: "test.mf",
    srcs: ["a.java", "b.kt"],
    java_resource_dirs: ["res"],
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
				"srcs": `[
        "a.java",
        "b.kt",
    ]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
			MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
				"main_class":   `"com.android.test.MainClass"`,
				"runtime_deps": `[":java-binary-host_kt"]`,
				"resources": `[
        "res/a.res",
        "res/dir1/b.res",
    ]`,
				"resource_strip_prefix": `"res"`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
}

func TestJavaBinaryHostKotlinWithResources(t *testing.T) {
	runJavaBinaryHostTestCase(t, Bp2buildTestCase{
		Description: "java_binary_host with srcs, libs, resources.",
		Filesystem: map[string]string{
			"test.mf":   "Main-Class: com.android.test.MainClass",
			"res/a.res": "",
			"res/b.res": "",
		},
		Blueprint: `java_binary_host {
    name: "java-binary-host",
    manifest: "test.mf",
    srcs: ["a.java", "b.kt"],
    java_resources: ["res/a.res", "res/b.res"],
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
				"srcs": `[
        "a.java",
        "b.kt",
    ]`,
				"resources": `[
        "res/a.res",
        "res/b.res",
    ]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
			MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
				"main_class":   `"com.android.test.MainClass"`,
				"runtime_deps": `[":java-binary-host_kt"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
}
+54 −16
Original line number Diff line number Diff line
@@ -2529,6 +2529,7 @@ type javaCommonAttributes struct {
	Srcs        bazel.LabelListAttribute
	Plugins     bazel.LabelListAttribute
	Javacopts   bazel.StringListAttribute
	Common_srcs bazel.LabelListAttribute
}

type javaDependencyLabels struct {
@@ -2717,7 +2718,6 @@ type javaLibraryAttributes struct {
	Deps      bazel.LabelListAttribute
	Exports   bazel.LabelListAttribute
	Neverlink bazel.BoolAttribute
	Common_srcs bazel.LabelListAttribute
}

func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
@@ -2751,7 +2751,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",
@@ -2807,14 +2807,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 {
@@ -2837,12 +2831,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)