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

Commit 2ed94c72 authored by Vinh Tran's avatar Vinh Tran Committed by Gerrit Code Review
Browse files

Merge "Support target.apex stanza in bp2build"

parents e32ed012 85fb07cf
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -69,6 +69,9 @@ const (

	AndroidAndInApex  = "android-in_apex"
	AndroidAndNonApex = "android-non_apex"

	InApex  = "in_apex"
	NonApex = "non_apex"
)

func PowerSetWithoutEmptySet[T any](items []T) [][]T {
@@ -198,6 +201,12 @@ var (
		AndroidAndNonApex:          "//build/bazel/rules/apex:android-non_apex",
		ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
	}

	inApexMap = map[string]string{
		InApex:                     "//build/bazel/rules/apex:in_apex",
		NonApex:                    "//build/bazel/rules/apex:non_apex",
		ConditionsDefaultConfigKey: ConditionsDefaultSelectKey,
	}
)

// basic configuration types
@@ -210,6 +219,7 @@ const (
	osArch
	productVariables
	osAndInApex
	inApex
)

func osArchString(os string, arch string) string {
@@ -224,6 +234,7 @@ func (ct configurationType) String() string {
		osArch:           "arch_os",
		productVariables: "product_variables",
		osAndInApex:      "os_in_apex",
		inApex:           "in_apex",
	}[ct]
}

@@ -251,6 +262,10 @@ func (ct configurationType) validateConfig(config string) {
		if _, ok := osAndInApexMap[config]; !ok {
			panic(fmt.Errorf("Unknown os+in_apex config: %s", config))
		}
	case inApex:
		if _, ok := inApexMap[config]; !ok {
			panic(fmt.Errorf("Unknown in_apex config: %s", config))
		}
	default:
		panic(fmt.Errorf("Unrecognized ConfigurationType %d", ct))
	}
@@ -276,6 +291,8 @@ func (ca ConfigurationAxis) SelectKey(config string) string {
		return fmt.Sprintf("%s:%s", productVariableBazelPackage, config)
	case osAndInApex:
		return osAndInApexMap[config]
	case inApex:
		return inApexMap[config]
	default:
		panic(fmt.Errorf("Unrecognized ConfigurationType %d", ca.configurationType))
	}
@@ -292,6 +309,8 @@ var (
	OsArchConfigurationAxis = ConfigurationAxis{configurationType: osArch}
	// An axis for os+in_apex-specific configurations
	OsAndInApexAxis = ConfigurationAxis{configurationType: osAndInApex}
	// An axis for in_apex-specific configurations
	InApexAxis = ConfigurationAxis{configurationType: inApex}
)

// ProductVariableConfigurationAxis returns an axis for the given product variable
+2 −2
Original line number Diff line number Diff line
@@ -720,7 +720,7 @@ func (lla *LabelListAttribute) SetSelectValue(axis ConfigurationAxis, config str
	switch axis.configurationType {
	case noConfig:
		lla.Value = list
	case arch, os, osArch, productVariables, osAndInApex:
	case arch, os, osArch, productVariables, osAndInApex, inApex:
		if lla.ConfigurableValues == nil {
			lla.ConfigurableValues = make(configurableLabelLists)
		}
@@ -736,7 +736,7 @@ func (lla *LabelListAttribute) SelectValue(axis ConfigurationAxis, config string
	switch axis.configurationType {
	case noConfig:
		return lla.Value
	case arch, os, osArch, productVariables, osAndInApex:
	case arch, os, osArch, productVariables, osAndInApex, inApex:
		return lla.ConfigurableValues[axis][config]
	default:
		panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
+83 −0
Original line number Diff line number Diff line
@@ -3037,3 +3037,86 @@ cc_library {
		},
	})
}

func TestCcLibraryWithTargetApex(t *testing.T) {
	runCcLibraryTestCase(t, Bp2buildTestCase{
		Description:                "cc_library with target.apex",
		ModuleTypeUnderTest:        "cc_library",
		ModuleTypeUnderTestFactory: cc.LibraryFactory,
		Blueprint: `
cc_library {
    name: "foo",
	shared_libs: ["bar", "baz"],
	static_libs: ["baz", "buh"],
	target: {
        apex: {
            exclude_shared_libs: ["bar"],
            exclude_static_libs: ["buh"],
        }
    }
}`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
				"implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
        "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
        "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"local_includes": `["."]`,
			}),
			MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
				"implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
        "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
        "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"local_includes": `["."]`,
			}),
		},
	})
}

func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
	runCcLibraryTestCase(t, Bp2buildTestCase{
		Description:                "cc_library with target.apex and export_shared|static_lib_headers",
		ModuleTypeUnderTest:        "cc_library",
		ModuleTypeUnderTestFactory: cc.LibraryFactory,
		Blueprint: `
cc_library_static {
    name: "foo",
	shared_libs: ["bar", "baz"],
    static_libs: ["abc"],
    export_shared_lib_headers: ["baz"],
    export_static_lib_headers: ["abc"],
	target: {
        apex: {
            exclude_shared_libs: ["baz", "bar"],
            exclude_static_libs: ["abc"],
        }
    }
}`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
				"implementation_dynamic_deps": `select({
        "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"dynamic_deps": `select({
        "//build/bazel/rules/apex:non_apex": [":baz__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"deps": `select({
        "//build/bazel/rules/apex:non_apex": [":abc__BP2BUILD__MISSING__DEP"],
        "//conditions:default": [],
    })`,
				"local_includes": `["."]`,
			}),
		},
	})
}
+52 −2
Original line number Diff line number Diff line
@@ -840,6 +840,38 @@ var (
	soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
)

// resolveTargetApex re-adds the shared and static libs in target.apex.exclude_shared|static_libs props to non-apex variant
// since all libs are already excluded by default
func (la *linkerAttributes) resolveTargetApexProp(ctx android.BazelConversionPathContext, isBinary bool, props *BaseLinkerProperties) {
	sharedLibsForNonApex := maybePartitionExportedAndImplementationsDeps(
		ctx,
		true,
		props.Target.Apex.Exclude_shared_libs,
		props.Export_shared_lib_headers,
		bazelLabelForSharedDeps,
	)
	dynamicDeps := la.dynamicDeps.SelectValue(bazel.InApexAxis, bazel.NonApex)
	implDynamicDeps := la.implementationDynamicDeps.SelectValue(bazel.InApexAxis, bazel.NonApex)
	(&dynamicDeps).Append(sharedLibsForNonApex.export)
	(&implDynamicDeps).Append(sharedLibsForNonApex.implementation)
	la.dynamicDeps.SetSelectValue(bazel.InApexAxis, bazel.NonApex, dynamicDeps)
	la.implementationDynamicDeps.SetSelectValue(bazel.InApexAxis, bazel.NonApex, implDynamicDeps)

	staticLibsForNonApex := maybePartitionExportedAndImplementationsDeps(
		ctx,
		!isBinary,
		props.Target.Apex.Exclude_static_libs,
		props.Export_static_lib_headers,
		bazelLabelForSharedDeps,
	)
	deps := la.deps.SelectValue(bazel.InApexAxis, bazel.NonApex)
	implDeps := la.implementationDeps.SelectValue(bazel.InApexAxis, bazel.NonApex)
	(&deps).Append(staticLibsForNonApex.export)
	(&implDeps).Append(staticLibsForNonApex.implementation)
	la.deps.SetSelectValue(bazel.InApexAxis, bazel.NonApex, deps)
	la.implementationDeps.SetSelectValue(bazel.InApexAxis, bazel.NonApex, implDeps)
}

func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
	// Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
	var axisFeatures []string
@@ -850,7 +882,15 @@ func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversion
	// https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
	staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))

	staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
	staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(
		ctx,
		!isBinary,
		staticLibs,
		// Exclude static libs in Exclude_static_libs and Target.Apex.Exclude_static_libs props
		append(props.Exclude_static_libs, props.Target.Apex.Exclude_static_libs...),
		props.Export_static_lib_headers,
		bazelLabelForStaticDepsExcludes,
	)

	headerLibs := android.FirstUniqueStrings(props.Header_libs)
	hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
@@ -882,9 +922,19 @@ func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversion
		la.usedSystemDynamicDepAsDynamicDep[el] = true
	}

	sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
	sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(
		ctx,
		!isBinary,
		sharedLibs,
		// Exclude shared libs in Exclude_shared_libs and Target.Apex.Exclude_shared_libs props
		append(props.Exclude_shared_libs, props.Target.Apex.Exclude_shared_libs...),
		props.Export_shared_lib_headers,
		bazelLabelForSharedDepsExcludes,
	)
	la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
	la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
	la.resolveTargetApexProp(ctx, isBinary, props)

	if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
		// If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be
		// used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and