Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moved uniqueSourceFileQueue in his own file
  • Loading branch information
cmaglie committed Jul 7, 2025
commit 53d45caf8a3fe30e4da3973945e27f7993ed181c
32 changes: 4 additions & 28 deletions internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"os/exec"
"regexp"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -262,15 +261,15 @@ func (l *SketchLibrariesDetector) findIncludes(
if err != nil {
return err
}
sourceFileQueue.push(mergedfile)
sourceFileQueue.Push(mergedfile)

l.queueSourceFilesFromFolder(sourceFileQueue, sketchBuildPath, false /* recurse */, sketchBuildPath, sketchBuildPath)
srcSubfolderPath := sketchBuildPath.Join("src")
if srcSubfolderPath.IsDir() {
l.queueSourceFilesFromFolder(sourceFileQueue, srcSubfolderPath, true /* recurse */, sketchBuildPath, sketchBuildPath)
}

for !sourceFileQueue.empty() {
for !sourceFileQueue.Empty() {
err := l.findIncludesUntilDone(ctx, cache, sourceFileQueue, buildProperties, librariesBuildPath, platformArch)
if err != nil {
cachePath.Remove()
Expand Down Expand Up @@ -306,7 +305,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
librariesBuildPath *paths.Path,
platformArch string,
) error {
sourceFile := sourceFileQueue.pop()
sourceFile := sourceFileQueue.Pop()
sourcePath := sourceFile.SourcePath()
targetFilePath := paths.NullPath()
depPath := sourceFile.DepfilePath()
Expand Down Expand Up @@ -446,7 +445,7 @@ func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
if err != nil {
return err
}
sourceFileQueue.push(sourceFile)
sourceFileQueue.Push(sourceFile)
}

return nil
Expand Down Expand Up @@ -690,26 +689,3 @@ func writeCache(cache *includeCache, path *paths.Path) error {
}
return nil
}

type uniqueSourceFileQueue []*sourceFile

func (queue *uniqueSourceFileQueue) push(value *sourceFile) {
if !queue.contains(value) {
*queue = append(*queue, value)
}
}

func (queue uniqueSourceFileQueue) contains(target *sourceFile) bool {
return slices.ContainsFunc(queue, target.Equals)
}

func (queue *uniqueSourceFileQueue) pop() *sourceFile {
old := *queue
x := old[0]
*queue = old[1:]
return x
}

func (queue uniqueSourceFileQueue) empty() bool {
return len(queue) == 0
}
34 changes: 33 additions & 1 deletion internal/arduino/builder/internal/detector/source_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

package detector

import "github.com/arduino/go-paths-helper"
import (
"slices"

"github.com/arduino/go-paths-helper"
)

type sourceFile struct {
// Path to the source file within the sketch/library root folder
Expand Down Expand Up @@ -90,3 +94,31 @@ func (f *sourceFile) ObjectPath() *paths.Path {
func (f *sourceFile) DepfilePath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".d")
}

// uniqueSourceFileQueue is a queue of source files that does not allow duplicates.
type uniqueSourceFileQueue []*sourceFile

// Push adds a source file to the queue if it is not already present.
func (queue *uniqueSourceFileQueue) Push(value *sourceFile) {
if !queue.Contains(value) {
*queue = append(*queue, value)
}
}

// Contains checks if the queue Contains a source file.
func (queue uniqueSourceFileQueue) Contains(target *sourceFile) bool {
return slices.ContainsFunc(queue, target.Equals)
}

// Pop removes and returns the first element of the queue.
func (queue *uniqueSourceFileQueue) Pop() *sourceFile {
old := *queue
x := old[0]
*queue = old[1:]
return x
}

// Empty returns true if the queue is Empty.
func (queue uniqueSourceFileQueue) Empty() bool {
return len(queue) == 0
}