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

Commit bdc60997 authored by Liz Kammer's avatar Liz Kammer
Browse files

Support autoconverted modules in mixed builds

modules converted with bp2build_available are will also be available to
be used in mixed builds.

Test: build/bazel/scripts/milestone-2/demo.sh full
Test: go tests
Change-Id: I49f16ec3ba5bb11dfed8066af069c27eb04371fb
parent ba3ea16f
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import (
	"path/filepath"
	"strings"

	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"
)

@@ -51,9 +52,11 @@ type BazelModuleBase struct {
type Bazelable interface {
	bazelProps() *properties
	HasHandcraftedLabel() bool
	GetBazelLabel() string
	HandcraftedLabel() string
	GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
	ConvertWithBp2build() bool
	GetBazelBuildFileContents(c Config, path, name string) (string, error)
	ConvertedToBazel() bool
}

// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
@@ -84,8 +87,14 @@ func (b *BazelModuleBase) HandcraftedLabel() string {
}

// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
func (b *BazelModuleBase) GetBazelLabel() string {
	return proptools.String(b.bazelProperties.Bazel_module.Label)
func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
	if b.HasHandcraftedLabel() {
		return b.HandcraftedLabel()
	}
	if b.ConvertWithBp2build() {
		return bp2buildModuleLabel(ctx, module)
	}
	return "" // no label for unconverted module
}

// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
@@ -98,8 +107,8 @@ func (b *BazelModuleBase) ConvertWithBp2build() bool {
// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
// something more targeted based on the rule type and target.
func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
	if !strings.Contains(b.GetBazelLabel(), path) {
		return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.GetBazelLabel())
	if !strings.Contains(b.HandcraftedLabel(), path) {
		return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
	}
	name = filepath.Join(path, name)
	f, err := c.fs.Open(name)
@@ -114,3 +123,9 @@ func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string)
	}
	return string(data[:]), nil
}

// ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically
// or manually
func (b *BazelModuleBase) ConvertedToBazel() bool {
	return b.ConvertWithBp2build() || b.HasHandcraftedLabel()
}
+38 −7
Original line number Diff line number Diff line
@@ -339,6 +339,7 @@ type BazelConversionPathContext interface {
	EarlyModulePathContext

	GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
	Module() Module
	OtherModuleName(m blueprint.Module) string
	OtherModuleDir(m blueprint.Module) string
}
@@ -434,15 +435,45 @@ func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes
// already be resolved by either deps mutator or path deps mutator.
func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string) bazel.Label {
	m, _ := ctx.GetDirectDep(dep)
	otherLabel := bazelModuleLabel(ctx, m, tag)
	label := bazelModuleLabel(ctx, ctx.Module(), "")
	if samePackage(label, otherLabel) {
		otherLabel = bazelShortLabel(otherLabel)
	}

	return bazel.Label{
		Label: otherLabel,
	}
}

func bazelModuleLabel(ctx BazelConversionPathContext, module blueprint.Module, tag string) string {
	// TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets.
	otherModuleName := ctx.OtherModuleName(m)
	var label bazel.Label
	if otherDir, dir := ctx.OtherModuleDir(m), ctx.ModuleDir(); otherDir != dir {
		label.Label = fmt.Sprintf("//%s:%s", otherDir, otherModuleName)
	} else {
		label.Label = fmt.Sprintf(":%s", otherModuleName)
	b, ok := module.(Bazelable)
	// TODO(b/181155349): perhaps return an error here if the module can't be/isn't being converted
	if !ok || !b.ConvertedToBazel() {
		return bp2buildModuleLabel(ctx, module)
	}
	return b.GetBazelLabel(ctx, module)
}
	return label

func bazelShortLabel(label string) string {
	i := strings.Index(label, ":")
	return label[i:]
}

func bazelPackage(label string) string {
	i := strings.Index(label, ":")
	return label[0:i]
}

func samePackage(label1, label2 string) bool {
	return bazelPackage(label1) == bazelPackage(label2)
}

func bp2buildModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
	moduleName := ctx.OtherModuleName(module)
	moduleDir := ctx.OtherModuleDir(module)
	return fmt.Sprintf("//%s:%s", moduleDir, moduleName)
}

// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
+1 −1
Original line number Diff line number Diff line
@@ -235,7 +235,7 @@ func GenerateBazelTargets(ctx *CodegenContext) (map[string]BazelTargets, Codegen
}

func getBazelPackagePath(b android.Bazelable) string {
	label := b.GetBazelLabel()
	label := b.HandcraftedLabel()
	pathToBuildFile := strings.TrimPrefix(label, "//")
	pathToBuildFile = strings.Split(pathToBuildFile, ":")[0]
	return pathToBuildFile
+1 −1
Original line number Diff line number Diff line
@@ -1605,7 +1605,7 @@ func (c *Module) setSubnameProperty(actx android.ModuleContext) {

// Returns true if Bazel was successfully used for the analysis of this module.
func (c *Module) maybeGenerateBazelActions(actx android.ModuleContext) bool {
	bazelModuleLabel := c.GetBazelLabel()
	bazelModuleLabel := c.GetBazelLabel(actx, c)
	bazelActionsUsed := false
	if c.bazelHandler != nil && actx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 {
		bazelActionsUsed = c.bazelHandler.generateBazelBuildActions(actx, bazelModuleLabel)
+1 −1
Original line number Diff line number Diff line
@@ -538,7 +538,7 @@ func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {

	g.outputFiles = outputFiles.Paths()

	bazelModuleLabel := g.GetBazelLabel()
	bazelModuleLabel := g.GetBazelLabel(ctx, g)
	bazelActionsUsed := false
	if ctx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 {
		bazelActionsUsed = g.generateBazelBuildActions(ctx, bazelModuleLabel)