Skip to content

Commit

Permalink
[skip-changelog] legacy: Builder refactorization (part 1...) (#2286)
Browse files Browse the repository at this point in the history
* Simplified compileFiles

* Starting new Builder (part 1/n)

* builder: renamed variable and moved dir creation up

* builder: made a sketch-prepress function part of Builder

* Removed ctx dependency in PreprocessSketchWithArduinoPreprocessor

* uniform parameters between preprocesors

* Moved PreprocessSketchWithArduinoPreprocessor into proper place

* Inlined function

* Converted sketchCopyAdditionalFiles into a Builder method

* Made SetupBuildProperties a method of the new Builder

* Refactor AddAdditionalEntriesToContext

cmaglie#32

* refactor AddAdditionalEntriesToContext in a function

* use the new function in all the tests

* Move the assignaton of LibrariesResolutionResults inside the ResolveLibrary func

The ResolveLibrary func is only called by the ContainerFindIncludes

* rename bPath to buildPath

* cleanup usless tests

* remove shadowed variable

* Refactor legacy LibrariesLoader command

* move LibrariesLoader under arduino/builder

* remove usless nil check

* remove AddAdditionalEntries func, in favour of initializing them in the compile command

* move a check directly in the compile command

* create the SketchLibrariesDetector struct

* move all the logic of ContainerSetupHardwareToolsLibsSketchAndProps in the compile command

* remove container_setup and adjust relative tests

* remove LibraryResolver property from context

* remove UseCachedLibrariesResolution for context

* remove ImportedLibraries from context

* remove LibrariesResolutionResults from context

* remove LibrariesManager from context

* fix regression exceeding 100% status bar

* refactor find_includes

* refactoring the cmd.Exec in favour of executils

* use detector FindIncludes in tests

* add comments and make some plubic methods private

---------

Co-authored-by: Alessio Perugini <[email protected]>
  • Loading branch information
cmaglie and alessio-perugini authored Sep 6, 2023
1 parent d106a67 commit 72c32ca
Show file tree
Hide file tree
Showing 45 changed files with 1,457 additions and 1,600 deletions.
31 changes: 10 additions & 21 deletions legacy/builder/types/accessories.go → arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,18 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package types
package builder

import "golang.org/x/exp/slices"
import "github.com/arduino/arduino-cli/arduino/sketch"

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)
// Builder is a Sketch builder.
type Builder struct {
sketch *sketch.Sketch
}

func (queue *UniqueSourceFileQueue) Pop() *SourceFile {
old := *queue
x := old[0]
*queue = old[1:]
return x
}

func (queue UniqueSourceFileQueue) Empty() bool {
return len(queue) == 0
// NewBuilder creates a sketch Builder.
func NewBuilder(sk *sketch.Sketch) *Builder {
return &Builder{
sketch: sk,
}
}
31 changes: 14 additions & 17 deletions arduino/builder/compilation_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"

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

Expand Down Expand Up @@ -67,25 +67,22 @@ func (db *CompilationDatabase) SaveToFile() {
}
}

func dirForCommand(command *exec.Cmd) string {
// This mimics what Cmd.Run also does: Use Dir if specified,
// current directory otherwise
if command.Dir != "" {
return command.Dir
}
dir, err := os.Getwd()
if err != nil {
fmt.Println(tr("Error getting current directory for compilation database: %s", err))
return ""
// Add adds a new CompilationDatabase entry
func (db *CompilationDatabase) Add(target *paths.Path, command *executils.Process) {
commandDir := command.GetDir()
if commandDir == "" {
// This mimics what Cmd.Run also does: Use Dir if specified,
// current directory otherwise
dir, err := os.Getwd()
if err != nil {
fmt.Println(tr("Error getting current directory for compilation database: %s", err))
}
commandDir = dir
}
return dir
}

// Add adds a new CompilationDatabase entry
func (db *CompilationDatabase) Add(target *paths.Path, command *exec.Cmd) {
entry := CompilationCommand{
Directory: dirForCommand(command),
Arguments: command.Args,
Directory: commandDir,
Arguments: command.GetArgs(),
File: target.String(),
}

Expand Down
5 changes: 3 additions & 2 deletions arduino/builder/compilation_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package builder

import (
"os/exec"
"testing"

"github.com/arduino/arduino-cli/executils"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)
Expand All @@ -28,7 +28,8 @@ func TestCompilationDatabase(t *testing.T) {
require.NoError(t, err)
defer tmpfile.Remove()

cmd := exec.Command("gcc", "arg1", "arg2")
cmd, err := executils.NewProcess(nil, "gcc", "arg1", "arg2")
require.NoError(t, err)
db := NewCompilationDatabase(tmpfile)
db.Add(paths.New("test"), cmd)
db.SaveToFile()
Expand Down
5 changes: 5 additions & 0 deletions arduino/builder/cpp/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ func ParseString(line string) (string, string, bool) {
i += width
}
}

// WrapWithHyphenI fixdoc
func WrapWithHyphenI(value string) string {
return "\"-I" + value + "\""
}
Loading

0 comments on commit 72c32ca

Please sign in to comment.