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

Commit c05e28c3 authored by Cole Faust's avatar Cole Faust Committed by Gerrit Code Review
Browse files

Merge "Remove old BUILD file merging code"

parents e66fe044 ea602c5a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -295,6 +295,7 @@ var (
		"external/bazel-skylib":/* recursive = */ true,
		"external/guava":/* recursive = */ true,
		"external/jsr305":/* recursive = */ true,
		"external/protobuf":/* recursive = */ false,
		"frameworks/ex/common":/* recursive = */ true,

		"packages/apps/Music":/* recursive = */ true,
@@ -302,6 +303,7 @@ var (

		"prebuilts/bazel":/* recursive = */ true,
		"prebuilts/bundletool":/* recursive = */ true,
		"prebuilts/clang/host/linux-x86":/* recursive = */ false,
		"prebuilts/gcc":/* recursive = */ true,
		"prebuilts/build-tools":/* recursive = */ true,
		"prebuilts/jdk/jdk11":/* recursive = */ false,
+0 −26
Original line number Diff line number Diff line
@@ -17,9 +17,6 @@ package android
import (
	"bufio"
	"errors"
	"fmt"
	"io/ioutil"
	"path/filepath"
	"strings"

	"github.com/google/blueprint"
@@ -117,7 +114,6 @@ type Bazelable interface {
	GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
	ShouldConvertWithBp2build(ctx BazelConversionContext) bool
	shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
	GetBazelBuildFileContents(c Config, path, name string) (string, error)
	ConvertWithBp2build(ctx TopDownMutatorContext)

	// namespacedVariableProps is a map from a soong config variable namespace
@@ -498,28 +494,6 @@ func bp2buildDefaultTrueRecursively(packagePath string, config allowlists.Bp2Bui
	return false, packagePath
}

// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
// an error if there are errors reading the file.
// 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.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)
	if err != nil {
		return "", err
	}
	defer f.Close()

	data, err := ioutil.ReadAll(f)
	if err != nil {
		return "", err
	}
	return string(data[:]), nil
}

func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
	ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel()
}
+2 −76
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ type BazelTarget struct {
	content         string
	ruleClass       string
	bzlLoadLocation string
	handcrafted     bool
}

// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
@@ -65,25 +64,9 @@ func (t BazelTarget) Label() string {
// BazelTargets is a typedef for a slice of BazelTarget objects.
type BazelTargets []BazelTarget

// HasHandcraftedTargetsreturns true if a set of bazel targets contain
// handcrafted ones.
func (targets BazelTargets) hasHandcraftedTargets() bool {
	for _, target := range targets {
		if target.handcrafted {
			return true
		}
	}
	return false
}

// sort a list of BazelTargets in-place, by name, and by generated/handcrafted types.
// sort a list of BazelTargets in-place by name
func (targets BazelTargets) sort() {
	sort.Slice(targets, func(i, j int) bool {
		if targets[i].handcrafted != targets[j].handcrafted {
			// Handcrafted targets will be generated after the bp2build generated targets.
			return targets[j].handcrafted
		}
		// This will cover all bp2build generated targets.
		return targets[i].name < targets[j].name
	})
}
@@ -94,18 +77,6 @@ func (targets BazelTargets) sort() {
func (targets BazelTargets) String() string {
	var res string
	for i, target := range targets {
		// There is only at most 1 handcrafted "target", because its contents
		// represent the entire BUILD file content from the tree. See
		// build_conversion.go#getHandcraftedBuildContent for more information.
		//
		// Add a header to make it easy to debug where the handcrafted targets
		// are in a generated BUILD file.
		if target.handcrafted {
			res += "# -----------------------------\n"
			res += "# Section: Handcrafted targets. \n"
			res += "# -----------------------------\n\n"
		}

		res += target.content
		if i != len(targets)-1 {
			res += "\n\n"
@@ -256,7 +227,6 @@ func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {

func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
	buildFileToTargets := make(map[string]BazelTargets)
	buildFileToAppend := make(map[string]bool)

	// Simple metrics tracking for bp2build
	metrics := CodegenMetrics{
@@ -288,30 +258,10 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers
				// Handle modules converted to handcrafted targets.
				//
				// Since these modules are associated with some handcrafted
				// target in a BUILD file, we simply append the entire contents
				// of that BUILD file to the generated BUILD file.
				//
				// The append operation is only done once, even if there are
				// multiple modules from the same directory associated to
				// targets in the same BUILD file (or package).
				// target in a BUILD file, we don't autoconvert them.

				// Log the module.
				metrics.AddConvertedModule(m, moduleType, Handcrafted)

				pathToBuildFile := getBazelPackagePath(b)
				if _, exists := buildFileToAppend[pathToBuildFile]; exists {
					// Append the BUILD file content once per package, at most.
					return
				}
				t, err := getHandcraftedBuildContent(ctx, b, pathToBuildFile)
				if err != nil {
					errs = append(errs, fmt.Errorf("Error converting %s: %s", bpCtx.ModuleName(m), err))
					return
				}
				targets = append(targets, t)
				// 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
				buildFileToAppend[pathToBuildFile] = true
			} else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
				// Handle modules converted to generated targets.

@@ -397,29 +347,6 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers
	}, errs
}

func getBazelPackagePath(b android.Bazelable) string {
	label := b.HandcraftedLabel()
	pathToBuildFile := strings.TrimPrefix(label, "//")
	pathToBuildFile = strings.Split(pathToBuildFile, ":")[0]
	return pathToBuildFile
}

func getHandcraftedBuildContent(ctx *CodegenContext, b android.Bazelable, pathToBuildFile string) (BazelTarget, error) {
	p := android.ExistentPathForSource(ctx, pathToBuildFile, HandcraftedBuildFileName)
	if !p.Valid() {
		return BazelTarget{}, fmt.Errorf("Could not find file %q for handcrafted target.", pathToBuildFile)
	}
	c, err := b.GetBazelBuildFileContents(ctx.Config(), pathToBuildFile, HandcraftedBuildFileName)
	if err != nil {
		return BazelTarget{}, err
	}
	// TODO(b/181575318): once this is more targeted, we need to include name, rule class, etc
	return BazelTarget{
		content:     c,
		handcrafted: true,
	}, nil
}

func generateBazelTargets(ctx bpToBuildContext, m android.Module) []BazelTarget {
	var targets []BazelTarget
	for _, m := range m.Bp2buildTargets() {
@@ -462,7 +389,6 @@ func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) BazelTarget {
			targetName,
			attributes,
		),
		handcrafted: false,
	}
}

+2 −11
Original line number Diff line number Diff line
@@ -1299,9 +1299,7 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
    name: "fg_foo",
    bazel_module: { label: "//other:fg_foo" },
}`,
			ExpectedBazelTargets: []string{
				`// BUILD file`,
			},
			ExpectedBazelTargets: []string{},
			Filesystem: map[string]string{
				"other/BUILD.bazel": `// BUILD file`,
			},
@@ -1319,9 +1317,7 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
        name: "foo",
        bazel_module: { label: "//other:foo" },
    }`,
			ExpectedBazelTargets: []string{
				`// BUILD file`,
			},
			ExpectedBazelTargets: []string{},
			Filesystem: map[string]string{
				"other/BUILD.bazel": `// BUILD file`,
			},
@@ -1349,7 +1345,6 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
			},
			ExpectedBazelTargets: []string{
				MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
				`// definition for fg_bar`,
			},
		},
		{
@@ -1375,7 +1370,6 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
    }`,
			ExpectedBazelTargets: []string{
				MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
				`// BUILD file`,
			},
		},
	}
@@ -1420,9 +1414,6 @@ func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
			if actualCount != expectedCount {
				t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
			}
			if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
				t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
			}
			for i, target := range bazelTargets {
				actualContent := target.content
				expectedContent := testCase.ExpectedBazelTargets[i]
+3 −11
Original line number Diff line number Diff line
@@ -96,17 +96,9 @@ func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode)
# This file was automatically generated by bp2build for the Bazel migration project.
# Feel free to edit or test it, but do *not* check it into your version control system.
`
			if targets.hasHandcraftedTargets() {
				// For BUILD files with both handcrafted and generated targets,
				// don't hardcode actual content, like package() declarations.
				// Leave that responsibility to the checked-in BUILD file
				// instead.
				content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
			} else {
				// For fully-generated BUILD files, hardcode the default visibility.
				content += "package(default_visibility = [\"//visibility:public\"])"
			}
			content += "\n"

			// Hardcode the default visibility.
			content += "package(default_visibility = [\"//visibility:public\"])\n"
			content += targets.LoadStatements()
		} else if mode == QueryView {
			content = soongModuleLoad