Skip to content

Commit

Permalink
Merge branch 'arduino:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
coby2023t authored Sep 10, 2023
2 parents 92800d5 + 65915d8 commit 0a4319f
Show file tree
Hide file tree
Showing 60 changed files with 1,702 additions and 1,426 deletions.
51 changes: 47 additions & 4 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,59 @@

package builder

import "github.com/arduino/arduino-cli/arduino/sketch"
import (
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
)

// Builder is a Sketch builder.
type Builder struct {
sketch *sketch.Sketch
sketch *sketch.Sketch
buildProperties *properties.Map

// core related
coreBuildCachePath *paths.Path
}

// NewBuilder creates a sketch Builder.
func NewBuilder(sk *sketch.Sketch) *Builder {
func NewBuilder(
sk *sketch.Sketch,
boardBuildProperties *properties.Map,
buildPath *paths.Path,
optimizeForDebug bool,
coreBuildCachePath *paths.Path,
) *Builder {
buildProperties := properties.NewMap()
if boardBuildProperties != nil {
buildProperties.Merge(boardBuildProperties)
}

if buildPath != nil {
buildProperties.SetPath("build.path", buildPath)
}
if sk != nil {
buildProperties.Set("build.project_name", sk.MainFile.Base())
buildProperties.SetPath("build.source.path", sk.FullPath)
}
if optimizeForDebug {
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
buildProperties.Set("compiler.optimization_flags", debugFlags)
}
} else {
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
buildProperties.Set("compiler.optimization_flags", releaseFlags)
}
}

return &Builder{
sketch: sk,
sketch: sk,
buildProperties: buildProperties,
coreBuildCachePath: coreBuildCachePath,
}
}

// GetBuildProperties returns the build properties for running this build
func (b *Builder) GetBuildProperties() *properties.Map {
return b.buildProperties
}
8 changes: 8 additions & 0 deletions arduino/builder/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package builder

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

// CoreBuildCachePath fixdoc
func (b *Builder) CoreBuildCachePath() *paths.Path {
return b.coreBuildCachePath
}
68 changes: 39 additions & 29 deletions arduino/builder/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ func (l *SketchLibrariesDetector) findIncludes(

if !l.useCachedLibrariesResolution {
sketch := sketch
mergedfile, err := makeSourceFile(sketchBuildPath, librariesBuildPath, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
mergedfile, err := makeSourceFile(sketchBuildPath, sketchBuildPath, paths.New(sketch.MainFile.Base()+".cpp"))
if err != nil {
return errors.WithStack(err)
}
sourceFileQueue.push(mergedfile)

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

for !sourceFileQueue.empty() {
Expand Down Expand Up @@ -419,20 +419,21 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
}
} else {
for _, sourceDir := range library.SourceDirs() {
l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, library, sourceDir.Dir, sourceDir.Recurse)
l.queueSourceFilesFromFolder(sourceFileQueue, sourceDir.Dir, sourceDir.Recurse,
library.SourceDir, librariesBuildPath.Join(library.DirName), library.UtilityDir)
}
}
first = false
}
}

func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
sketchBuildPath *paths.Path,
librariesBuildPath *paths.Path,
sourceFileQueue *uniqueSourceFileQueue,
origin interface{},
folder *paths.Path,
recurse bool,
sourceDir *paths.Path,
buildDir *paths.Path,
extraIncludePath ...*paths.Path,
) error {
sourceFileExtensions := []string{}
for k := range globals.SourceFilesValidExtensions {
Expand All @@ -444,7 +445,7 @@ func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
}

for _, filePath := range filePaths {
sourceFile, err := makeSourceFile(sketchBuildPath, librariesBuildPath, origin, filePath)
sourceFile, err := makeSourceFile(sourceDir, buildDir, filePath, extraIncludePath...)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -537,33 +538,42 @@ func (f *sourceFile) Equals(g *sourceFile) bool {
// given origin. The given path can be absolute, or relative within the
// origin's root source folder
func makeSourceFile(
sketchBuildPath *paths.Path,
librariesBuildPath *paths.Path,
origin interface{},
path *paths.Path,
sourceDir *paths.Path,
buildDir *paths.Path,
sourceFilePath *paths.Path,
extraIncludePath ...*paths.Path,
) (*sourceFile, error) {
res := &sourceFile{}

switch o := origin.(type) {
case *sketch.Sketch:
res.buildRoot = sketchBuildPath
res.sourceRoot = sketchBuildPath
case *libraries.Library:
res.buildRoot = librariesBuildPath.Join(o.DirName)
res.sourceRoot = o.SourceDir
res.extraIncludePath = o.UtilityDir
default:
panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
}

if path.IsAbs() {
res := &sourceFile{
buildRoot: buildDir,
sourceRoot: sourceDir,
}

if len(extraIncludePath) > 1 {
panic("only one extra include path allowed")
}
if len(extraIncludePath) > 0 {
res.extraIncludePath = extraIncludePath[0]
}
// switch o := origin.(type) {
// case *sketch.Sketch:
// res.buildRoot = sketchBuildPath
// res.sourceRoot = sketchBuildPath
// case *libraries.Library:
// res.buildRoot = librariesBuildPath.Join(o.DirName)
// res.sourceRoot = o.SourceDir
// res.extraIncludePath = o.UtilityDir
// default:
// panic("Unexpected origin for SourceFile: " + fmt.Sprint(origin))
// }

if sourceFilePath.IsAbs() {
var err error
path, err = res.sourceRoot.RelTo(path)
sourceFilePath, err = res.sourceRoot.RelTo(sourceFilePath)
if err != nil {
return nil, err
}
}
res.relativePath = path
res.relativePath = sourceFilePath
return res, nil
}

Expand Down
33 changes: 33 additions & 0 deletions arduino/builder/progress/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package progress

// Struct fixdoc
type Struct struct {
Progress float32
StepAmount float32
Parent *Struct
}

// AddSubSteps fixdoc
func (p *Struct) AddSubSteps(steps int) {
p.Parent = &Struct{
Progress: p.Progress,
StepAmount: p.StepAmount,
Parent: p.Parent,
}
if p.StepAmount == 0.0 {
p.StepAmount = 100.0
}
p.StepAmount /= float32(steps)
}

// RemoveSubSteps fixdoc
func (p *Struct) RemoveSubSteps() {
p.Progress = p.Parent.Progress
p.StepAmount = p.Parent.StepAmount
p.Parent = p.Parent.Parent
}

// CompleteStep fixdoc
func (p *Struct) CompleteStep() {
p.Progress += p.StepAmount
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// 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 progress

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ import (
)

func TestProgress(t *testing.T) {
p := &ProgressStruct{}
p := &Struct{}
p.AddSubSteps(3)
require.Equal(t, float32(0.0), p.Progress)
require.InEpsilon(t, 33.33333, p.StepAmount, 0.00001)
Expand Down
26 changes: 26 additions & 0 deletions arduino/builder/sizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package builder

import rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"

// ExecutableSectionSize represents a section of the executable output file
type ExecutableSectionSize struct {
Name string `json:"name"`
Size int `json:"size"`
MaxSize int `json:"max_size"`
}

// ExecutablesFileSections is an array of ExecutablesFileSection
type ExecutablesFileSections []ExecutableSectionSize

// ToRPCExecutableSectionSizeArray transforms this array into a []*rpc.ExecutableSectionSize
func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.ExecutableSectionSize {
res := []*rpc.ExecutableSectionSize{}
for _, section := range s {
res = append(res, &rpc.ExecutableSectionSize{
Name: section.Name,
Size: int64(section.Size),
MaxSize: int64(section.MaxSize),
})
}
return res
}
27 changes: 0 additions & 27 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -166,29 +165,3 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error {
// Source and destination are the same, don't write anything
return nil
}

// SetupBuildProperties adds the build properties related to the sketch to the
// default board build properties map.
func (b *Builder) SetupBuildProperties(boardBuildProperties *properties.Map, buildPath *paths.Path, optimizeForDebug bool) *properties.Map {
buildProperties := properties.NewMap()
buildProperties.Merge(boardBuildProperties)

if buildPath != nil {
buildProperties.SetPath("build.path", buildPath)
}
if b.sketch != nil {
buildProperties.Set("build.project_name", b.sketch.MainFile.Base())
buildProperties.SetPath("build.source.path", b.sketch.FullPath)
}
if optimizeForDebug {
if debugFlags, ok := buildProperties.GetOk("compiler.optimization_flags.debug"); ok {
buildProperties.Set("compiler.optimization_flags", debugFlags)
}
} else {
if releaseFlags, ok := buildProperties.GetOk("compiler.optimization_flags.release"); ok {
buildProperties.Set("compiler.optimization_flags", releaseFlags)
}
}

return buildProperties
}
6 changes: 3 additions & 3 deletions arduino/builder/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) {
}
mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource)

b := NewBuilder(sk)
b := NewBuilder(sk, nil, nil, false, nil)
offset, source, err := b.sketchMergeSources(nil)
require.Nil(t, err)
require.Equal(t, 2, offset)
Expand All @@ -61,7 +61,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {
require.NotNil(t, sk)

// ensure not to include Arduino.h when it's already there
b := NewBuilder(sk)
b := NewBuilder(sk, nil, nil, false, nil)
_, source, err := b.sketchMergeSources(nil)
require.Nil(t, err)
require.Equal(t, 1, strings.Count(source, "<Arduino.h>"))
Expand All @@ -76,7 +76,7 @@ func TestCopyAdditionalFiles(t *testing.T) {
sk1, err := sketch.New(paths.New("testdata", t.Name()))
require.Nil(t, err)
require.Equal(t, sk1.AdditionalFiles.Len(), 1)
b1 := NewBuilder(sk1)
b1 := NewBuilder(sk1, nil, nil, false, nil)

// copy the sketch over, create a fake main file we don't care about it
// but we need it for `SketchLoad` to succeed later
Expand Down
Loading

0 comments on commit 0a4319f

Please sign in to comment.