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

Commit 75539d62 authored by Sam Delmerico's avatar Sam Delmerico
Browse files

add target_compatible_with stanza for host targets

Soong modules that are specific for the host platform (e.g.
java_library_host, cc_binary_host, java_genrule_host, etc.) should
not be built on the target platform (Android), so we add a
target_compatible_with attribute to skip this type of module on an
Android target build.

Bug: 215229742
Test: go test ./bp2build
Change-Id: Ifb76ef4e0dc4cb3adb6a64b5c375ce36f7973e48
parent d519b331
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1165,6 +1165,11 @@ func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutator
		productConfigEnabledLabels, nil,
	})

	moduleSupportsDevice := mod.commonProperties.HostOrDeviceSupported&deviceSupported == deviceSupported
	if mod.commonProperties.HostOrDeviceSupported != NeitherHostNorDeviceSupported && !moduleSupportsDevice {
		enabledProperty.SetSelectValue(bazel.OsConfigurationAxis, Android.Name, proptools.BoolPtr(false))
	}

	platformEnabledAttribute, err := enabledProperty.ToLabelListAttribute(
		bazel.LabelList{[]bazel.Label{bazel.Label{Label: "@platforms//:incompatible"}}, nil},
		bazel.LabelList{[]bazel.Label{}, nil})
+4 −4
Original line number Diff line number Diff line
@@ -84,13 +84,13 @@ func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
	t.Helper()
	testCase := tc
	for i, tar := range testCase.targets {
		if tar.typ != "cc_binary" {
			continue
		}
		switch tar.typ {
		case "cc_binary", "proto_library", "cc_lite_proto_library":
			tar.attrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}
		testCase.targets[i] = tar
	}
	moduleTypeUnderTest := "cc_binary_host"
+110 −42
Original line number Diff line number Diff line
@@ -97,13 +97,22 @@ func TestGenruleCliVariableReplacement(t *testing.T) {
}`

	for _, tc := range testCases {
		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", attrNameToString{
		moduleAttrs := attrNameToString{
			"cmd":   fmt.Sprintf(`"$(location :foo.tool) --genDir=%s arg $(SRCS) $(OUTS)"`, tc.genDir),
			"outs":  `["foo.out"]`,
			"srcs":  `["foo.in"]`,
			"tools": `[":foo.tool"]`,
			}),
		}

		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

		t.Run(tc.moduleType, func(t *testing.T) {
@@ -158,25 +167,36 @@ func TestGenruleLocationsLabel(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets :=
		[]string{
			makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		fooAttrs := attrNameToString{
			"cmd":   `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
			"outs":  `["foo.out"]`,
			"srcs":  `["foo.in"]`,
			"tools": `[":foo.tools"]`,
			}),
			makeBazelTarget("genrule", "foo.tools", attrNameToString{
		}
		fooToolsAttrs := attrNameToString{
			"cmd": `"cp $(SRCS) $(OUTS)"`,
			"outs": `[
        "foo_tool.out",
        "foo_tool2.out",
    ]`,
			"srcs": `["foo_tool.in"]`,
			}),
		}

	for _, tc := range testCases {
		if tc.moduleType == "java_genrule_host" {
			compatibilityAttrs := `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
			fooAttrs["target_compatible_with"] = compatibilityAttrs
			fooToolsAttrs["target_compatible_with"] = compatibilityAttrs
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", fooAttrs),
			makeBazelTarget("genrule", "foo.tools", fooToolsAttrs),
		}

		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
@@ -221,16 +241,25 @@ func TestGenruleLocationsAbsoluteLabel(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets := []string{
		makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		moduleAttrs := attrNameToString{
			"cmd":   `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
			"outs":  `["foo.out"]`,
			"srcs":  `["foo.in"]`,
			"tools": `["//other:foo.tool"]`,
		}),
		}

	for _, tc := range testCases {
		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
@@ -276,16 +305,25 @@ func TestGenruleSrcsLocationsAbsoluteLabel(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets := []string{
		makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		moduleAttrs := attrNameToString{
			"cmd":   `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
			"outs":  `["foo.out"]`,
			"srcs":  `["//other:other.tool"]`,
			"tools": `["//other:foo.tool"]`,
		}),
		}

	for _, tc := range testCases {
		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
@@ -331,8 +369,8 @@ func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets := []string{
		makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		moduleAttrs := attrNameToString{
			"cmd":  `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
			"outs": `["foo.out"]`,
			"srcs": `["foo.in"]`,
@@ -340,9 +378,19 @@ func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) {
        "//other:foo.tool",
        "//other:other.tool",
    ]`,
		})}
		}

		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

	for _, tc := range testCases {
		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
@@ -388,8 +436,8 @@ func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets := []string{
		makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		moduleAttrs := attrNameToString{
			"cmd":  `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
			"outs": `["foo.out"]`,
			"srcs": `["foo.in"]`,
@@ -397,9 +445,19 @@ func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) {
        "//other:foo.tool",
        "//other:other.tool",
    ]`,
		})}
		}

		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

	for _, tc := range testCases {
		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
@@ -444,14 +502,24 @@ func TestGenruleWithoutToolsOrToolFiles(t *testing.T) {
    bazel_module: { bp2build_available: true },
}`

	expectedBazelTargets := []string{
		makeBazelTarget("genrule", "foo", attrNameToString{
	for _, tc := range testCases {
		moduleAttrs := attrNameToString{
			"cmd":  `"cp $(SRCS) $(OUTS)"`,
			"outs": `["foo.out"]`,
			"srcs": `["foo.in"]`,
		})}
		}

		if tc.moduleType == "java_genrule_host" {
			moduleAttrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
		}

		expectedBazelTargets := []string{
			makeBazelTarget("genrule", "foo", moduleAttrs),
		}

	for _, tc := range testCases {
		t.Run(tc.moduleType, func(t *testing.T) {
			runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
				bp2buildTestCase{
+4 −0
Original line number Diff line number Diff line
@@ -59,6 +59,10 @@ func TestJavaBinaryHost(t *testing.T) {
				"deps":       `["//other:jni-lib-1"]`,
				"jvm_flags":  `["-Djava.library.path=$${RUNPATH}other"]`,
				"javacopts":  `["-Xdoclint:all/protected"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
+8 −0
Original line number Diff line number Diff line
@@ -48,9 +48,17 @@ java_library_host {
			makeBazelTarget("java_library", "java-lib-host-1", attrNameToString{
				"srcs": `["a.java"]`,
				"deps": `[":java-lib-host-2"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
			makeBazelTarget("java_library", "java-lib-host-2", attrNameToString{
				"srcs": `["c.java"]`,
				"target_compatible_with": `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`,
			}),
		},
	})
Loading