Skip to content

Commit 22a0249

Browse files
move PrepareCommandForRecipe in builder struct
1 parent f836ac2 commit 22a0249

File tree

9 files changed

+61
-61
lines changed

9 files changed

+61
-61
lines changed

Diff for: arduino/builder/archive_compiled_files.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (b *Builder) archiveCompiledFiles(buildPath *paths.Path, archiveFile *paths
4747
properties.SetPath("archive_file_path", archiveFilePath)
4848
properties.SetPath("object_file", objectFile)
4949

50-
command, err := utils.PrepareCommandForRecipe(properties, "recipe.ar.pattern", false)
50+
command, err := b.prepareCommandForRecipe(properties, "recipe.ar.pattern", false)
5151
if err != nil {
5252
return nil, errors.WithStack(err)
5353
}

Diff for: arduino/builder/builder.go

+48
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
"errors"
2020
"fmt"
2121
"io"
22+
"os"
23+
"path/filepath"
24+
"strings"
2225

2326
"github.com/arduino/arduino-cli/arduino/builder/internal/compilation"
2427
"github.com/arduino/arduino-cli/arduino/builder/internal/detector"
@@ -28,6 +31,7 @@ import (
2831
"github.com/arduino/arduino-cli/arduino/libraries"
2932
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
3033
"github.com/arduino/arduino-cli/arduino/sketch"
34+
"github.com/arduino/arduino-cli/executils"
3135
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3236
"github.com/arduino/go-paths-helper"
3337
"github.com/arduino/go-properties-orderedmap"
@@ -480,3 +484,47 @@ func (b *Builder) build() error {
480484
}
481485
return nil
482486
}
487+
488+
func (b *Builder) prepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool) (*executils.Process, error) {
489+
pattern := buildProperties.Get(recipe)
490+
if pattern == "" {
491+
return nil, fmt.Errorf(tr("%[1]s pattern is missing"), recipe)
492+
}
493+
494+
commandLine := buildProperties.ExpandPropsInString(pattern)
495+
if removeUnsetProperties {
496+
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
497+
}
498+
499+
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
500+
if err != nil {
501+
return nil, err
502+
}
503+
504+
// if the overall commandline is too long for the platform
505+
// try reducing the length by making the filenames relative
506+
// and changing working directory to build.path
507+
var relativePath string
508+
if len(commandLine) > 30000 {
509+
relativePath = buildProperties.Get("build.path")
510+
for i, arg := range parts {
511+
if _, err := os.Stat(arg); os.IsNotExist(err) {
512+
continue
513+
}
514+
rel, err := filepath.Rel(relativePath, arg)
515+
if err == nil && !strings.Contains(rel, "..") && len(rel) < len(arg) {
516+
parts[i] = rel
517+
}
518+
}
519+
}
520+
521+
command, err := executils.NewProcess(nil, parts...)
522+
if err != nil {
523+
return nil, err
524+
}
525+
if relativePath != "" {
526+
command.SetDir(relativePath)
527+
}
528+
529+
return command, nil
530+
}

Diff for: arduino/builder/compilation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (b *Builder) compileFileWithRecipe(
125125
return nil, errors.WithStack(err)
126126
}
127127

128-
command, err := utils.PrepareCommandForRecipe(properties, recipe, false)
128+
command, err := b.prepareCommandForRecipe(properties, recipe, false)
129129
if err != nil {
130130
return nil, errors.WithStack(err)
131131
}

Diff for: arduino/builder/export_cmake.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ func (b *Builder) exportProjectCMake(importedLibraries libraries.List, includeFo
275275
var dynamicLibsFromGccMinusL []string
276276
var linkDirectories []string
277277

278-
extractCompileFlags(b.buildProperties, "recipe.c.combine.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
279-
extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
280-
extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
278+
b.extractCompileFlags(b.buildProperties, "recipe.c.combine.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
279+
b.extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
280+
b.extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)
281281

282282
// Extract folders with .h in them for adding in include list
283283
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...)
@@ -348,7 +348,7 @@ func (b *Builder) exportProjectCMake(importedLibraries libraries.List, includeFo
348348
return nil
349349
}
350350

351-
func extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
351+
func (b *Builder) extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
352352
appendIfNotPresent := func(target []string, elements ...string) []string {
353353
for _, element := range elements {
354354
if !slices.Contains(target, element) {
@@ -358,7 +358,7 @@ func extractCompileFlags(buildProperties *properties.Map, recipe string, defines
358358
return target
359359
}
360360

361-
command, _ := utils.PrepareCommandForRecipe(buildProperties, recipe, true)
361+
command, _ := b.prepareCommandForRecipe(buildProperties, recipe, true)
362362

363363
for _, arg := range command.GetArgs() {
364364
if strings.HasPrefix(arg, "-D") {

Diff for: arduino/builder/internal/utils/utils.go

-47
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import (
1919
"bytes"
2020
"io"
2121
"os"
22-
"path/filepath"
2322
"strings"
2423
"unicode"
2524

2625
"github.com/arduino/arduino-cli/executils"
2726
"github.com/arduino/arduino-cli/i18n"
2827
f "github.com/arduino/arduino-cli/internal/algorithms"
2928
"github.com/arduino/go-paths-helper"
30-
"github.com/arduino/go-properties-orderedmap"
3129
"github.com/pkg/errors"
3230
"github.com/sirupsen/logrus"
3331
"golang.org/x/text/runes"
@@ -295,48 +293,3 @@ func DirContentIsOlderThan(dir *paths.Path, target *paths.Path, extensions ...st
295293
}
296294
return true, nil
297295
}
298-
299-
// PrepareCommandForRecipe fixdoc
300-
func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool) (*executils.Process, error) {
301-
pattern := buildProperties.Get(recipe)
302-
if pattern == "" {
303-
return nil, errors.Errorf(tr("%[1]s pattern is missing"), recipe)
304-
}
305-
306-
commandLine := buildProperties.ExpandPropsInString(pattern)
307-
if removeUnsetProperties {
308-
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
309-
}
310-
311-
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
312-
if err != nil {
313-
return nil, errors.WithStack(err)
314-
}
315-
316-
// if the overall commandline is too long for the platform
317-
// try reducing the length by making the filenames relative
318-
// and changing working directory to build.path
319-
var relativePath string
320-
if len(commandLine) > 30000 {
321-
relativePath = buildProperties.Get("build.path")
322-
for i, arg := range parts {
323-
if _, err := os.Stat(arg); os.IsNotExist(err) {
324-
continue
325-
}
326-
rel, err := filepath.Rel(relativePath, arg)
327-
if err == nil && !strings.Contains(rel, "..") && len(rel) < len(arg) {
328-
parts[i] = rel
329-
}
330-
}
331-
}
332-
333-
command, err := executils.NewProcess(nil, parts...)
334-
if err != nil {
335-
return nil, errors.WithStack(err)
336-
}
337-
if relativePath != "" {
338-
command.SetDir(relativePath)
339-
}
340-
341-
return command, nil
342-
}

Diff for: arduino/builder/libraries.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/arduino/arduino-cli/arduino/builder/cpp"
24-
"github.com/arduino/arduino-cli/arduino/builder/internal/utils"
2524
"github.com/arduino/arduino-cli/arduino/libraries"
2625
f "github.com/arduino/arduino-cli/internal/algorithms"
2726
"github.com/arduino/go-paths-helper"
@@ -68,7 +67,7 @@ func (b *Builder) findExpectedPrecompiledLibFolder(
6867
// Add fpu specifications if they exist
6968
// To do so, resolve recipe.cpp.o.pattern,
7069
// search for -mfpu=xxx -mfloat-abi=yyy and add to a subfolder
71-
command, _ := utils.PrepareCommandForRecipe(buildProperties, "recipe.cpp.o.pattern", true)
70+
command, _ := b.prepareCommandForRecipe(buildProperties, "recipe.cpp.o.pattern", true)
7271
fpuSpecs := ""
7372
for _, el := range command.GetArgs() {
7473
if strings.Contains(el, FpuCflag) {

Diff for: arduino/builder/linker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (b *Builder) link() error {
7272
properties.SetPath("archive_file_path", archive)
7373
properties.SetPath("object_file", object)
7474

75-
command, err := utils.PrepareCommandForRecipe(properties, "recipe.ar.pattern", false)
75+
command, err := b.prepareCommandForRecipe(properties, "recipe.ar.pattern", false)
7676
if err != nil {
7777
return errors.WithStack(err)
7878
}
@@ -96,7 +96,7 @@ func (b *Builder) link() error {
9696
properties.Set("archive_file_path", b.buildArtifacts.coreArchiveFilePath.String())
9797
properties.Set("object_files", objectFileList)
9898

99-
command, err := utils.PrepareCommandForRecipe(properties, "recipe.c.combine.pattern", false)
99+
command, err := b.prepareCommandForRecipe(properties, "recipe.c.combine.pattern", false)
100100
if err != nil {
101101
return err
102102
}

Diff for: arduino/builder/recipe.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (b *Builder) RunRecipe(prefix, suffix string, skipIfOnlyUpdatingCompilation
3939
for _, recipe := range recipes {
4040
logrus.Debugf(fmt.Sprintf("Running recipe: %s", recipe))
4141

42-
command, err := utils.PrepareCommandForRecipe(properties, recipe, false)
42+
command, err := b.prepareCommandForRecipe(properties, recipe, false)
4343
if err != nil {
4444
return errors.WithStack(err)
4545
}

Diff for: arduino/builder/sizer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (b *Builder) size() error {
7272
}
7373

7474
func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) {
75-
command, err := utils.PrepareCommandForRecipe(b.buildProperties, "recipe.advanced_size.pattern", false)
75+
command, err := b.prepareCommandForRecipe(b.buildProperties, "recipe.advanced_size.pattern", false)
7676
if err != nil {
7777
return nil, errors.New(tr("Error while determining sketch size: %s", err))
7878
}
@@ -204,7 +204,7 @@ func (b *Builder) checkSize() (ExecutablesFileSections, error) {
204204
}
205205

206206
func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
207-
command, err := utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false)
207+
command, err := b.prepareCommandForRecipe(properties, "recipe.size.pattern", false)
208208
if err != nil {
209209
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
210210
return

0 commit comments

Comments
 (0)