Snap for 11889377 from c124dbb8e3 to 24Q3-release

Change-Id: I6a15e5e55b94457f7ee58dfee406a9f3037b6844
This commit is contained in:
Android Build Coastguard Worker 2024-05-25 03:00:24 +00:00
commit f3292cfa77
5 changed files with 101 additions and 53 deletions

View file

@ -28,6 +28,7 @@ import (
"strings"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
type Args struct {
@ -190,14 +191,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf
providerValidationErrors := <-providersValidationChan
if providerValidationErrors != nil {
var sb strings.Builder
for i, err := range providerValidationErrors {
if i != 0 {
sb.WriteString("\n")
}
sb.WriteString(err.Error())
}
return nil, errors.New(sb.String())
return nil, proptools.MergeErrors(providerValidationErrors)
}
if args.Memprofile != "" {

View file

@ -38,7 +38,6 @@ import (
"sync/atomic"
"text/scanner"
"text/template"
"time"
"unsafe"
"github.com/google/blueprint/metrics"
@ -4144,23 +4143,50 @@ func (c *Context) VerifyProvidersWereUnchanged() []error {
if !c.buildActionsReady {
return []error{ErrBuildActionsNotReady}
}
var errors []error
for _, m := range c.modulesSorted {
for i, provider := range m.providers {
if provider != nil {
hash, err := proptools.HashProvider(provider)
if err != nil {
errors = append(errors, fmt.Errorf("provider %q on module %q was modified after being set, and no longer hashable afterwards: %s", providerRegistry[i].typ, m.Name(), err.Error()))
continue
}
if provider != nil && m.providerInitialValueHashes[i] != hash {
errors = append(errors, fmt.Errorf("provider %q on module %q was modified after being set", providerRegistry[i].typ, m.Name()))
}
} else if m.providerInitialValueHashes[i] != 0 {
// This should be unreachable, because in setProvider we check if the provider has already been set.
errors = append(errors, fmt.Errorf("provider %q on module %q was unset somehow, this is an internal error", providerRegistry[i].typ, m.Name()))
}
toProcess := make(chan *moduleInfo)
errorCh := make(chan []error)
var wg sync.WaitGroup
go func() {
for _, m := range c.modulesSorted {
toProcess <- m
}
close(toProcess)
}()
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
var errors []error
for m := range toProcess {
for i, provider := range m.providers {
if provider != nil {
hash, err := proptools.HashProvider(provider)
if err != nil {
errors = append(errors, fmt.Errorf("provider %q on module %q was modified after being set, and no longer hashable afterwards: %s", providerRegistry[i].typ, m.Name(), err.Error()))
continue
}
if m.providerInitialValueHashes[i] != hash {
errors = append(errors, fmt.Errorf("provider %q on module %q was modified after being set", providerRegistry[i].typ, m.Name()))
}
} else if m.providerInitialValueHashes[i] != 0 {
// This should be unreachable, because in setProvider we check if the provider has already been set.
errors = append(errors, fmt.Errorf("provider %q on module %q was unset somehow, this is an internal error", providerRegistry[i].typ, m.Name()))
}
}
}
if errors != nil {
errorCh <- errors
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(errorCh)
}()
var errors []error
for newErrors := range errorCh {
errors = append(errors, newErrors...)
}
return errors
}
@ -4509,42 +4535,52 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter, shardNinja bool, ninjaF
}
if shardNinja {
var wg sync.WaitGroup
errorCh := make(chan error)
fileNames := GetNinjaShardFiles(ninjaFileName)
shardedModules := proptools.ShardByCount(modules, len(fileNames))
ninjaShardCnt := len(shardedModules)
files := GetNinjaShardFiles(ninjaFileName)
shardedModules := proptools.ShardByCount(modules, len(files))
for i, batchModules := range shardedModules {
go func() {
f, err := os.OpenFile(filepath.Join(c.SrcDir(), fileNames[i]), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions)
file := files[i]
wg.Add(1)
go func(file string, batchModules []*moduleInfo) {
defer wg.Done()
f, err := os.OpenFile(filepath.Join(c.SrcDir(), file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions)
if err != nil {
errorCh <- fmt.Errorf("error opening Ninja file: %s", err)
return
}
defer f.Close()
buf := bufio.NewWriterSize(f, 16*1024*1024)
defer buf.Flush()
writer := newNinjaWriter(buf)
errorCh <- c.writeModuleAction(batchModules, writer, headerTemplate)
}()
nw.Subninja(fileNames[i])
}
if ninjaShardCnt > 0 {
afterCh := time.After(60 * time.Second)
count := 1
for {
select {
case err := <-errorCh:
defer func() {
err := f.Close()
if err != nil {
return err
} else if count == ninjaShardCnt {
return nil
errorCh <- err
}
count++
case <-afterCh:
return fmt.Errorf("timed out when writing ninja file")
}()
buf := bufio.NewWriterSize(f, 16*1024*1024)
defer func() {
err := buf.Flush()
if err != nil {
errorCh <- err
}
}()
writer := newNinjaWriter(buf)
err = c.writeModuleAction(batchModules, writer, headerTemplate)
if err != nil {
errorCh <- err
}
}
}(file, batchModules)
nw.Subninja(file)
}
go func() {
wg.Wait()
close(errorCh)
}()
var errors []error
for newErrors := range errorCh {
errors = append(errors, newErrors)
}
if len(errors) > 0 {
return proptools.MergeErrors(errors)
}
return nil
} else {

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/google/blueprint
go 1.21
go 1.22

View file

@ -390,6 +390,7 @@ func (p *parser) evaluateOperator(value1, value2 Expression, operator rune,
Cases: []*SelectCase{{
Value: e1,
}},
ExpressionType: e1.Type(),
}
}
}

View file

@ -1,7 +1,9 @@
package proptools
import (
"errors"
"math"
"strings"
)
func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T {
@ -23,3 +25,18 @@ func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T {
func ShardByCount[T ~[]E, E any](toShard T, shardCount int) []T {
return ShardBySize(toShard, int(math.Ceil(float64(len(toShard))/float64(shardCount))))
}
// MergeErrors merges a list of errors into a single error.
func MergeErrors(errs []error) error {
if errs != nil {
var sb strings.Builder
for i, err := range errs {
if i != 0 {
sb.WriteString("\n")
}
sb.WriteString(err.Error())
}
return errors.New(sb.String())
}
return nil
}