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

Commit 1c841c4c authored by Colin Cross's avatar Colin Cross Committed by Gerrit Code Review
Browse files

Merge changes from topic "move-depset" into main

* changes:
  Update DepSet references
  Move DepSet to blueprint
  Convert DepSet to a wrapper around a pointer
parents 830f56a7 a14fb6a7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ bootstrap_go_package {
    name: "soong-aidl-library",
    pkgPath: "android/soong/aidl_library",
    deps: [
        "blueprint-depset",
        "soong-android",
    ],
    srcs: [
+9 −8
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package aidl_library
import (
	"android/soong/android"
	"github.com/google/blueprint"
	"github.com/google/blueprint/depset"
	"github.com/google/blueprint/proptools"
)

@@ -58,17 +59,17 @@ type AidlLibraryInfo struct {
	// The direct aidl files of the module
	Srcs android.Paths
	// The include dirs to the direct aidl files and those provided from transitive aidl_library deps
	IncludeDirs android.DepSet[android.Path]
	IncludeDirs depset.DepSet[android.Path]
	// The direct hdrs and hdrs from transitive deps
	Hdrs android.DepSet[android.Path]
	Hdrs depset.DepSet[android.Path]
}

// AidlLibraryProvider provides the srcs and the transitive include dirs
var AidlLibraryProvider = blueprint.NewProvider[AidlLibraryInfo]()

func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	includeDirsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
	hdrsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
	includeDirsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)
	hdrsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)

	if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 {
		ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty")
@@ -100,15 +101,15 @@ func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {

	for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
		if info, ok := android.OtherModuleProvider(ctx, dep, AidlLibraryProvider); ok {
			includeDirsDepSetBuilder.Transitive(&info.IncludeDirs)
			hdrsDepSetBuilder.Transitive(&info.Hdrs)
			includeDirsDepSetBuilder.Transitive(info.IncludeDirs)
			hdrsDepSetBuilder.Transitive(info.Hdrs)
		}
	}

	android.SetProvider(ctx, AidlLibraryProvider, AidlLibraryInfo{
		Srcs:        srcs,
		IncludeDirs: *includeDirsDepSetBuilder.Build(),
		Hdrs:        *hdrsDepSetBuilder.Build(),
		IncludeDirs: includeDirsDepSetBuilder.Build(),
		Hdrs:        hdrsDepSetBuilder.Build(),
	})
}

+1 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ bootstrap_go_package {
    deps: [
        "blueprint",
        "blueprint-bootstrap",
        "blueprint-depset",
        "blueprint-metrics",
        "sbox_proto",
        "soong",
@@ -50,7 +51,6 @@ bootstrap_go_package {
        "deapexer.go",
        "defaults.go",
        "defs.go",
        "depset_generic.go",
        "deptag.go",
        "dirgroup.go",
        "early_module_context.go",
@@ -123,7 +123,6 @@ bootstrap_go_package {
        "configured_jars_test.go",
        "csuite_config_test.go",
        "defaults_test.go",
        "depset_test.go",
        "deptag_test.go",
        "expand_test.go",
        "filegroup_test.go",

android/depset_generic.go

deleted100644 → 0
+0 −226
Original line number Diff line number Diff line
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package android

import (
	"fmt"

	"github.com/google/blueprint"
)

// DepSet is designed to be conceptually compatible with Bazel's depsets:
// https://docs.bazel.build/versions/master/skylark/depsets.html

type DepSetOrder int

const (
	PREORDER DepSetOrder = iota
	POSTORDER
	TOPOLOGICAL
)

func (o DepSetOrder) String() string {
	switch o {
	case PREORDER:
		return "PREORDER"
	case POSTORDER:
		return "POSTORDER"
	case TOPOLOGICAL:
		return "TOPOLOGICAL"
	default:
		panic(fmt.Errorf("Invalid DepSetOrder %d", o))
	}
}

type depSettableType comparable

// A DepSet efficiently stores a slice of an arbitrary type from transitive dependencies without
// copying. It is stored as a DAG of DepSet nodes, each of which has some direct contents and a list
// of dependency DepSet nodes.
//
// A DepSet has an order that will be used to walk the DAG when ToList() is called.  The order
// can be POSTORDER, PREORDER, or TOPOLOGICAL.  POSTORDER and PREORDER orders return a postordered
// or preordered left to right flattened list.  TOPOLOGICAL returns a list that guarantees that
// elements of children are listed after all of their parents (unless there are duplicate direct
// elements in the DepSet or any of its transitive dependencies, in which case the ordering of the
// duplicated element is not guaranteed).
//
// A DepSet is created by NewDepSet or NewDepSetBuilder.Build from the slice for direct contents
// and the *DepSets of dependencies. A DepSet is immutable once created.
type DepSet[T depSettableType] struct {
	preorder   bool
	reverse    bool
	order      DepSetOrder
	direct     []T
	transitive []*DepSet[T]
}

type depSetGob[T depSettableType] struct {
	Preorder   bool
	Reverse    bool
	Order      DepSetOrder
	Direct     []T
	Transitive []*DepSet[T]
}

func (d *DepSet[T]) ToGob() *depSetGob[T] {
	return &depSetGob[T]{
		Preorder:   d.preorder,
		Reverse:    d.reverse,
		Order:      d.order,
		Direct:     d.direct,
		Transitive: d.transitive,
	}
}

func (d *DepSet[T]) FromGob(data *depSetGob[T]) {
	d.preorder = data.Preorder
	d.reverse = data.Reverse
	d.order = data.Order
	d.direct = data.Direct
	d.transitive = data.Transitive
}

func (d *DepSet[T]) GobEncode() ([]byte, error) {
	return blueprint.CustomGobEncode[depSetGob[T]](d)
}

func (d *DepSet[T]) GobDecode(data []byte) error {
	return blueprint.CustomGobDecode[depSetGob[T]](data, d)
}

// NewDepSet returns an immutable DepSet with the given order, direct and transitive contents.
func NewDepSet[T depSettableType](order DepSetOrder, direct []T, transitive []*DepSet[T]) *DepSet[T] {
	var directCopy []T
	var transitiveCopy []*DepSet[T]
	for _, t := range transitive {
		if t.order != order {
			panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s",
				order, t.order))
		}
	}

	if order == TOPOLOGICAL {
		// TOPOLOGICAL is implemented as a postorder traversal followed by reversing the output.
		// Pre-reverse the inputs here so their order is maintained in the output.
		directCopy = ReverseSlice(direct)
		transitiveCopy = ReverseSlice(transitive)
	} else {
		directCopy = append([]T(nil), direct...)
		transitiveCopy = append([]*DepSet[T](nil), transitive...)
	}

	return &DepSet[T]{
		preorder:   order == PREORDER,
		reverse:    order == TOPOLOGICAL,
		order:      order,
		direct:     directCopy,
		transitive: transitiveCopy,
	}
}

// DepSetBuilder is used to create an immutable DepSet.
type DepSetBuilder[T depSettableType] struct {
	order      DepSetOrder
	direct     []T
	transitive []*DepSet[T]
}

// NewDepSetBuilder returns a DepSetBuilder to create an immutable DepSet with the given order and
// type, represented by a slice of type that will be in the DepSet.
func NewDepSetBuilder[T depSettableType](order DepSetOrder) *DepSetBuilder[T] {
	return &DepSetBuilder[T]{
		order: order,
	}
}

// DirectSlice adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct
// contents are to the right of any existing direct contents.
func (b *DepSetBuilder[T]) DirectSlice(direct []T) *DepSetBuilder[T] {
	b.direct = append(b.direct, direct...)
	return b
}

// Direct adds direct contents to the DepSet being built by a DepSetBuilder. Newly added direct
// contents are to the right of any existing direct contents.
func (b *DepSetBuilder[T]) Direct(direct ...T) *DepSetBuilder[T] {
	b.direct = append(b.direct, direct...)
	return b
}

// Transitive adds transitive contents to the DepSet being built by a DepSetBuilder. Newly added
// transitive contents are to the right of any existing transitive contents.
func (b *DepSetBuilder[T]) Transitive(transitive ...*DepSet[T]) *DepSetBuilder[T] {
	for _, t := range transitive {
		if t.order != b.order {
			panic(fmt.Errorf("incompatible order, new DepSet is %s but transitive DepSet is %s",
				b.order, t.order))
		}
	}
	b.transitive = append(b.transitive, transitive...)
	return b
}

// Returns the DepSet being built by this DepSetBuilder.  The DepSetBuilder retains its contents
// for creating more depSets.
func (b *DepSetBuilder[T]) Build() *DepSet[T] {
	return NewDepSet(b.order, b.direct, b.transitive)
}

// walk calls the visit method in depth-first order on a DepSet, preordered if d.preorder is set,
// otherwise postordered.
func (d *DepSet[T]) walk(visit func([]T)) {
	visited := make(map[*DepSet[T]]bool)

	var dfs func(d *DepSet[T])
	dfs = func(d *DepSet[T]) {
		visited[d] = true
		if d.preorder {
			visit(d.direct)
		}
		for _, dep := range d.transitive {
			if !visited[dep] {
				dfs(dep)
			}
		}

		if !d.preorder {
			visit(d.direct)
		}
	}

	dfs(d)
}

// ToList returns the DepSet flattened to a list.  The order in the list is based on the order
// of the DepSet.  POSTORDER and PREORDER orders return a postordered or preordered left to right
// flattened list.  TOPOLOGICAL returns a list that guarantees that elements of children are listed
// after all of their parents (unless there are duplicate direct elements in the DepSet or any of
// its transitive dependencies, in which case the ordering of the duplicated element is not
// guaranteed).
func (d *DepSet[T]) ToList() []T {
	if d == nil {
		return nil
	}
	var list []T
	d.walk(func(paths []T) {
		list = append(list, paths...)
	})
	list = firstUniqueInPlace(list)
	if d.reverse {
		ReverseSliceInPlace(list)
	}
	return list
}

android/depset_test.go

deleted100644 → 0
+0 −295
Original line number Diff line number Diff line
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package android

import (
	"fmt"
	"reflect"
	"strings"
	"testing"
)

func ExampleDepSet_ToList_postordered() {
	a := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("a")).Build()
	b := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("b")).Transitive(a).Build()
	c := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("c")).Transitive(a).Build()
	d := NewDepSetBuilder[Path](POSTORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()

	fmt.Println(Paths(d.ToList()).Strings())
	// Output: [a b c d]
}

func ExampleDepSet_ToList_preordered() {
	a := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("a")).Build()
	b := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("b")).Transitive(a).Build()
	c := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("c")).Transitive(a).Build()
	d := NewDepSetBuilder[Path](PREORDER).Direct(PathForTesting("d")).Transitive(b, c).Build()

	fmt.Println(Paths(d.ToList()).Strings())
	// Output: [d b a c]
}

func ExampleDepSet_ToList_topological() {
	a := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("a")).Build()
	b := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("b")).Transitive(a).Build()
	c := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("c")).Transitive(a).Build()
	d := NewDepSetBuilder[Path](TOPOLOGICAL).Direct(PathForTesting("d")).Transitive(b, c).Build()

	fmt.Println(Paths(d.ToList()).Strings())
	// Output: [d b c a]
}

// Tests based on Bazel's ExpanderTestBase.java to ensure compatibility
// https://github.com/bazelbuild/bazel/blob/master/src/test/java/com/google/devtools/build/lib/collect/nestedset/ExpanderTestBase.java
func TestDepSet(t *testing.T) {
	a := PathForTesting("a")
	b := PathForTesting("b")
	c := PathForTesting("c")
	c2 := PathForTesting("c2")
	d := PathForTesting("d")
	e := PathForTesting("e")

	tests := []struct {
		name                             string
		depSet                           func(t *testing.T, order DepSetOrder) *DepSet[Path]
		postorder, preorder, topological []string
	}{
		{
			name: "simple",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				return NewDepSet[Path](order, Paths{c, a, b}, nil)
			},
			postorder:   []string{"c", "a", "b"},
			preorder:    []string{"c", "a", "b"},
			topological: []string{"c", "a", "b"},
		},
		{
			name: "simpleNoDuplicates",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				return NewDepSet[Path](order, Paths{c, a, a, a, b}, nil)
			},
			postorder:   []string{"c", "a", "b"},
			preorder:    []string{"c", "a", "b"},
			topological: []string{"c", "a", "b"},
		},
		{
			name: "nesting",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				subset := NewDepSet[Path](order, Paths{c, a, e}, nil)
				return NewDepSet[Path](order, Paths{b, d}, []*DepSet[Path]{subset})
			},
			postorder:   []string{"c", "a", "e", "b", "d"},
			preorder:    []string{"b", "d", "c", "a", "e"},
			topological: []string{"b", "d", "c", "a", "e"},
		},
		{
			name: "builderReuse",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				assertEquals := func(t *testing.T, w, g Paths) {
					t.Helper()
					if !reflect.DeepEqual(w, g) {
						t.Errorf("want %q, got %q", w, g)
					}
				}
				builder := NewDepSetBuilder[Path](order)
				assertEquals(t, nil, builder.Build().ToList())

				builder.Direct(b)
				assertEquals(t, Paths{b}, builder.Build().ToList())

				builder.Direct(d)
				assertEquals(t, Paths{b, d}, builder.Build().ToList())

				child := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
				builder.Transitive(child)
				return builder.Build()
			},
			postorder:   []string{"c", "a", "e", "b", "d"},
			preorder:    []string{"b", "d", "c", "a", "e"},
			topological: []string{"b", "d", "c", "a", "e"},
		},
		{
			name: "builderChaining",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				return NewDepSetBuilder[Path](order).Direct(b).Direct(d).
					Transitive(NewDepSetBuilder[Path](order).Direct(c, a, e).Build()).Build()
			},
			postorder:   []string{"c", "a", "e", "b", "d"},
			preorder:    []string{"b", "d", "c", "a", "e"},
			topological: []string{"b", "d", "c", "a", "e"},
		},
		{
			name: "transitiveDepsHandledSeparately",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				subset := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
				builder := NewDepSetBuilder[Path](order)
				// The fact that we add the transitive subset between the Direct(b) and Direct(d)
				// calls should not change the result.
				builder.Direct(b)
				builder.Transitive(subset)
				builder.Direct(d)
				return builder.Build()
			},
			postorder:   []string{"c", "a", "e", "b", "d"},
			preorder:    []string{"b", "d", "c", "a", "e"},
			topological: []string{"b", "d", "c", "a", "e"},
		},
		{
			name: "nestingNoDuplicates",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				subset := NewDepSetBuilder[Path](order).Direct(c, a, e).Build()
				return NewDepSetBuilder[Path](order).Direct(b, d, e).Transitive(subset).Build()
			},
			postorder:   []string{"c", "a", "e", "b", "d"},
			preorder:    []string{"b", "d", "e", "c", "a"},
			topological: []string{"b", "d", "c", "a", "e"},
		},
		{
			name: "chain",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				c := NewDepSetBuilder[Path](order).Direct(c).Build()
				b := NewDepSetBuilder[Path](order).Direct(b).Transitive(c).Build()
				a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Build()

				return a
			},
			postorder:   []string{"c", "b", "a"},
			preorder:    []string{"a", "b", "c"},
			topological: []string{"a", "b", "c"},
		},
		{
			name: "diamond",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				d := NewDepSetBuilder[Path](order).Direct(d).Build()
				c := NewDepSetBuilder[Path](order).Direct(c).Transitive(d).Build()
				b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Build()
				a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()

				return a
			},
			postorder:   []string{"d", "b", "c", "a"},
			preorder:    []string{"a", "b", "d", "c"},
			topological: []string{"a", "b", "c", "d"},
		},
		{
			name: "extendedDiamond",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				d := NewDepSetBuilder[Path](order).Direct(d).Build()
				e := NewDepSetBuilder[Path](order).Direct(e).Build()
				b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Transitive(e).Build()
				c := NewDepSetBuilder[Path](order).Direct(c).Transitive(e).Transitive(d).Build()
				a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()
				return a
			},
			postorder:   []string{"d", "e", "b", "c", "a"},
			preorder:    []string{"a", "b", "d", "e", "c"},
			topological: []string{"a", "b", "c", "e", "d"},
		},
		{
			name: "extendedDiamondRightArm",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				d := NewDepSetBuilder[Path](order).Direct(d).Build()
				e := NewDepSetBuilder[Path](order).Direct(e).Build()
				b := NewDepSetBuilder[Path](order).Direct(b).Transitive(d).Transitive(e).Build()
				c2 := NewDepSetBuilder[Path](order).Direct(c2).Transitive(e).Transitive(d).Build()
				c := NewDepSetBuilder[Path](order).Direct(c).Transitive(c2).Build()
				a := NewDepSetBuilder[Path](order).Direct(a).Transitive(b).Transitive(c).Build()
				return a
			},
			postorder:   []string{"d", "e", "b", "c2", "c", "a"},
			preorder:    []string{"a", "b", "d", "e", "c", "c2"},
			topological: []string{"a", "b", "c", "c2", "e", "d"},
		},
		{
			name: "orderConflict",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				child1 := NewDepSetBuilder[Path](order).Direct(a, b).Build()
				child2 := NewDepSetBuilder[Path](order).Direct(b, a).Build()
				parent := NewDepSetBuilder[Path](order).Transitive(child1).Transitive(child2).Build()
				return parent
			},
			postorder:   []string{"a", "b"},
			preorder:    []string{"a", "b"},
			topological: []string{"b", "a"},
		},
		{
			name: "orderConflictNested",
			depSet: func(t *testing.T, order DepSetOrder) *DepSet[Path] {
				a := NewDepSetBuilder[Path](order).Direct(a).Build()
				b := NewDepSetBuilder[Path](order).Direct(b).Build()
				child1 := NewDepSetBuilder[Path](order).Transitive(a).Transitive(b).Build()
				child2 := NewDepSetBuilder[Path](order).Transitive(b).Transitive(a).Build()
				parent := NewDepSetBuilder[Path](order).Transitive(child1).Transitive(child2).Build()
				return parent
			},
			postorder:   []string{"a", "b"},
			preorder:    []string{"a", "b"},
			topological: []string{"b", "a"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Run("postorder", func(t *testing.T) {
				depSet := tt.depSet(t, POSTORDER)
				if g, w := Paths(depSet.ToList()).Strings(), tt.postorder; !reflect.DeepEqual(g, w) {
					t.Errorf("expected ToList() = %q, got %q", w, g)
				}
			})
			t.Run("preorder", func(t *testing.T) {
				depSet := tt.depSet(t, PREORDER)
				if g, w := Paths(depSet.ToList()).Strings(), tt.preorder; !reflect.DeepEqual(g, w) {
					t.Errorf("expected ToList() = %q, got %q", w, g)
				}
			})
			t.Run("topological", func(t *testing.T) {
				depSet := tt.depSet(t, TOPOLOGICAL)
				if g, w := Paths(depSet.ToList()).Strings(), tt.topological; !reflect.DeepEqual(g, w) {
					t.Errorf("expected ToList() = %q, got %q", w, g)
				}
			})
		})
	}
}

func TestDepSetInvalidOrder(t *testing.T) {
	orders := []DepSetOrder{POSTORDER, PREORDER, TOPOLOGICAL}

	run := func(t *testing.T, order1, order2 DepSetOrder) {
		defer func() {
			if r := recover(); r != nil {
				if err, ok := r.(error); !ok {
					t.Fatalf("expected panic error, got %v", err)
				} else if !strings.Contains(err.Error(), "incompatible order") {
					t.Fatalf("expected incompatible order error, got %v", err)
				}
			}
		}()
		NewDepSet(order1, nil, []*DepSet[Path]{NewDepSet[Path](order2, nil, nil)})
		t.Fatal("expected panic")
	}

	for _, order1 := range orders {
		t.Run(order1.String(), func(t *testing.T) {
			for _, order2 := range orders {
				t.Run(order2.String(), func(t *testing.T) {
					if order1 != order2 {
						run(t, order1, order2)
					}
				})
			}
		})
	}
}
Loading