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

Commit 855cfc2f authored by Yu Liu's avatar Yu Liu
Browse files

Convert cc_aconfig_library to bazel.

Bug: 297358249
Test: Unit test and CI
Change-Id: Ic84128b0df16efe4255b52b83670ec9902c09383
parent f2d858e5
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ package aconfig

import (
	"android/soong/android"
	"android/soong/bazel"
	"android/soong/cc"

	"github.com/google/blueprint"
@@ -31,6 +32,8 @@ type ccDeclarationsTagType struct {

var ccDeclarationsTag = ccDeclarationsTagType{}

const baseLibDep = "server_configurable_flags"

type CcAconfigLibraryProperties struct {
	// name of the aconfig_declarations module to generate a library for
	Aconfig_declarations string
@@ -72,7 +75,7 @@ func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc
	}

	// Add a dependency for the aconfig flags base library
	deps.SharedLibs = append(deps.SharedLibs, "server_configurable_flags")
	deps.SharedLibs = append(deps.SharedLibs, baseLibDep)
	// TODO: It'd be really nice if we could reexport this library and not make everyone do it.

	return deps
@@ -138,3 +141,33 @@ func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContex
		},
	})
}

type bazelCcAconfigLibraryAttributes struct {
	Aconfig_declarations bazel.LabelAttribute
	Dynamic_deps         bazel.LabelListAttribute
}

// Convert the cc_aconfig_library module to bazel.
//
// This method is called from cc.ConvertWithBp2build to actually convert the
// cc_aconfig_library module. This is necessary since the factory method of this
// module type returns a cc library and the bp2build conversion is called on the
// cc library type.

func (this *CcAconfigLibraryCallbacks) GeneratorBp2build(ctx android.Bp2buildMutatorContext) bool {
	if ctx.ModuleType() != "cc_aconfig_library" {
		return false
	}

	attrs := bazelCcAconfigLibraryAttributes{
		Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, this.properties.Aconfig_declarations).Label),
		Dynamic_deps:         bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, []string{baseLibDep})),
	}
	props := bazel.BazelTargetModuleProperties{
		Rule_class:        "cc_aconfig_library",
		Bzl_load_location: "//build/bazel/rules/cc:cc_aconfig_library.bzl",
	}

	ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs)
	return true
}
+1 −0
Original line number Diff line number Diff line
@@ -922,6 +922,7 @@ var (
		"aconfig_values",
		"aidl_interface_headers",
		"bpf",
		"cc_aconfig_library",
		"cc_prebuilt_library",
		"cc_prebuilt_library_headers",
		"cc_prebuilt_library_shared",
+1 −1
Original line number Diff line number Diff line
@@ -200,7 +200,7 @@ func (c Config) ReleaseVersion() string {
// The aconfig value set passed to aconfig, derived from RELEASE_VERSION
func (c Config) ReleaseAconfigValueSets() string {
	// This logic to handle both Soong module name and bazel target is temporary in order to
	// provide backward compatibility where aosp and vendor/google both have the release
	// provide backward compatibility where aosp and internal both have the release
	// aconfig value set but can't be updated at the same time to use bazel target
	value := strings.Split(c.config.productVariables.ReleaseAconfigValueSets, ":")
	value_len := len(value)
+45 −0
Original line number Diff line number Diff line
@@ -19,10 +19,12 @@ import (

	"android/soong/aconfig"
	"android/soong/android"
	"android/soong/cc"
)

func registerAconfigModuleTypes(ctx android.RegistrationContext) {
	aconfig.RegisterBuildComponents(ctx)
	ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
}

func TestAconfigDeclarations(t *testing.T) {
@@ -90,3 +92,46 @@ func TestAconfigValues(t *testing.T) {
		ExpectedBazelTargets: expectedBazelTargets,
	})
}

func TestCcAconfigLibrary(t *testing.T) {
	bp := `
	aconfig_declarations {
		name: "foo_aconfig_declarations",
		srcs: [
			"foo1.aconfig",
		],
		package: "com.android.foo",
	}
	cc_library {
			name: "server_configurable_flags",
			srcs: ["bar.cc"],
			bazel_module: { bp2build_available: false },
	}
	cc_aconfig_library {
			name: "foo",
			aconfig_declarations: "foo_aconfig_declarations",
	}
	`
	expectedBazelTargets := []string{
		MakeBazelTargetNoRestrictions(
			"aconfig_declarations",
			"foo_aconfig_declarations",
			AttrNameToString{
				"srcs":    `["foo1.aconfig"]`,
				"package": `"com.android.foo"`,
			},
		),
		MakeBazelTargetNoRestrictions(
			"cc_aconfig_library",
			"foo",
			AttrNameToString{
				"aconfig_declarations":   `":foo_aconfig_declarations"`,
				"dynamic_deps":           `[":server_configurable_flags"]`,
				"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
			},
		)}
	RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{
		Blueprint:            bp,
		ExpectedBazelTargets: expectedBazelTargets,
	})
}
+11 −0
Original line number Diff line number Diff line
@@ -589,6 +589,7 @@ type Generator interface {
	GeneratorFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
	GeneratorSources(ctx ModuleContext) GeneratedSource
	GeneratorBuildActions(ctx ModuleContext, flags Flags, deps PathDeps)
	GeneratorBp2build(ctx android.Bp2buildMutatorContext) bool
}

// compiler is the interface for a compiler helper object. Different module decorators may implement
@@ -4215,6 +4216,16 @@ func (c *Module) typ() moduleType {

// ConvertWithBp2build converts Module to Bazel for bp2build.
func (c *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
	if len(c.generators) > 0 {
		allConverted := true
		for _, generator := range c.generators {
			allConverted = allConverted && generator.GeneratorBp2build(ctx)
		}
		if allConverted {
			return
		}
	}

	prebuilt := c.IsPrebuilt()
	switch c.typ() {
	case binary:
Loading