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

Commit 4e00b092 authored by Liz Kammer's avatar Liz Kammer
Browse files

Don't use java_library for java_import neverlink

Restricting use of sdk_version = "none" to only the rule types that will
correctly handled sdk_version = "none" by also handling system_module
attr

Test: CI && go tests
Change-Id: Ifa1c60ba8f5e3fcb28986cc84cdfaedcbd2d2957
parent ba5f32a2
Loading
Loading
Loading
Loading
+29 −21
Original line number Diff line number Diff line
@@ -52,13 +52,15 @@ java_import {
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
				"srcs": `["import.jar"]`,
			}),
			MakeBazelTarget("java_import", "example_import", AttrNameToString{
				"jars": `["import.jar"]`,
				"jars": `[":example_import-jars"]`,
			}),
			MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
				"exports":     `[":example_import"]`,
			MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
				"jars":      `[":example_import-jars"]`,
				"neverlink": `True`,
				"sdk_version": `"none"`,
			}),
		}})
}
@@ -86,17 +88,19 @@ java_import {
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("java_import", "example_import", AttrNameToString{
				"jars": `select({
			MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
				"srcs": `select({
        "//build/bazel_common_rules/platforms/os:android": ["android.jar"],
        "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.jar"],
        "//conditions:default": [],
    })`,
			}),
			MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
				"exports":     `[":example_import"]`,
			MakeBazelTarget("java_import", "example_import", AttrNameToString{
				"jars": `[":example_import-jars"]`,
			}),
			MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
				"jars":      `[":example_import-jars"]`,
				"neverlink": `True`,
				"sdk_version": `"none"`,
			}),
		}})
}
@@ -117,13 +121,15 @@ java_import_host {
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
				"srcs": `["import.jar"]`,
			}),
			MakeBazelTarget("java_import", "example_import", AttrNameToString{
				"jars": `["import.jar"]`,
				"jars": `[":example_import-jars"]`,
			}),
			MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
				"exports":     `[":example_import"]`,
			MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
				"jars":      `[":example_import-jars"]`,
				"neverlink": `True`,
				"sdk_version": `"none"`,
			}),
		}})
}
@@ -142,13 +148,15 @@ func TestJavaImportSameNameAsJavaLibrary(t *testing.T) {
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("filegroup", "test_lib-jars", AttrNameToString{
				"srcs": `["test.jar"]`,
			}),
			MakeBazelTarget("java_import", "test_lib", AttrNameToString{
				"jars": `["test.jar"]`,
				"jars": `[":test_lib-jars"]`,
			}),
			MakeBazelTarget("java_library", "test_lib-neverlink", AttrNameToString{
				"exports":     `[":test_lib"]`,
			MakeBazelTarget("java_import", "test_lib-neverlink", AttrNameToString{
				"jars":      `[":test_lib-jars"]`,
				"neverlink": `True`,
				"sdk_version": `"none"`,
			}),
		},
	}, func(ctx android.RegistrationContext) {
+28 −15
Original line number Diff line number Diff line
@@ -3403,9 +3403,14 @@ func createLibraryTarget(ctx android.Bp2buildMutatorContext, libInfo libraryCrea
	return libName
}

type bazelJavaImportAttributes struct {
type importAttributes struct {
	Jars      bazel.LabelListAttribute
	Exports   bazel.LabelListAttribute
	Neverlink *bool
}

type filegroupAttrs struct {
	Srcs bazel.LabelListAttribute
}

// java_import bp2Build converter.
@@ -3421,28 +3426,36 @@ func (i *Import) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
		}
	}

	attrs := &bazelJavaImportAttributes{
		Jars: jars,
	name := android.RemoveOptionalPrebuiltPrefix(i.Name())
	filegroupTargetName := name + "-jars"

	ctx.CreateBazelTargetModule(
		bazel.BazelTargetModuleProperties{
			Rule_class:        "filegroup",
			Bzl_load_location: "//build/bazel/rules:filegroup.bzl",
		},
		android.CommonAttributes{Name: filegroupTargetName},
		&filegroupAttrs{
			Srcs: jars,
		},
	)

	attrs := &importAttributes{
		Jars: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + filegroupTargetName}),
	}
	props := bazel.BazelTargetModuleProperties{
		Rule_class:        "java_import",
		Bzl_load_location: "//build/bazel/rules/java:import.bzl",
	}

	name := android.RemoveOptionalPrebuiltPrefix(i.Name())

	ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)

	neverlink := true
	neverlinkAttrs := &javaLibraryAttributes{
		Neverlink: bazel.BoolAttribute{Value: &neverlink},
		Exports:   bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}),
		javaCommonAttributes: &javaCommonAttributes{
			Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")},
		},
	neverlinkAttrs := &importAttributes{
		Jars:      attrs.Jars,
		Neverlink: proptools.BoolPtr(true),
	}
	ctx.CreateBazelTargetModule(
		javaLibraryBazelTargetModuleProperties(),
		props,
		android.CommonAttributes{Name: name + "-neverlink"},
		neverlinkAttrs)
}