Skip to content

Commit

Permalink
refactoring the cmd.Exec in favour of executils
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 4, 2023
1 parent d97cac7 commit a64582c
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 61 deletions.
8 changes: 4 additions & 4 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 @@ -68,8 +68,8 @@ func (db *CompilationDatabase) SaveToFile() {
}

// Add adds a new CompilationDatabase entry
func (db *CompilationDatabase) Add(target *paths.Path, command *exec.Cmd) {
commandDir := command.Dir
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
Expand All @@ -82,7 +82,7 @@ func (db *CompilationDatabase) Add(target *paths.Path, command *exec.Cmd) {

entry := CompilationCommand{
Directory: commandDir,
Arguments: command.Args,
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
4 changes: 4 additions & 0 deletions arduino/builder/cpp/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ func ParseString(line string) (string, string, bool) {
i += width
}
}

func WrapWithHyphenI(value string) string {

Check failure on line 110 in arduino/builder/cpp/cpp.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported function WrapWithHyphenI should have comment or be unexported

Check failure on line 110 in arduino/builder/cpp/cpp.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported function WrapWithHyphenI should have comment or be unexported
return "\"-I" + value + "\""
}
4 changes: 2 additions & 2 deletions arduino/builder/preprocessor/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"
"strings"

"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/executils"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
Expand All @@ -38,7 +38,7 @@ func GCC(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.
gccBuildProperties.SetPath("source_file", sourceFilePath)
gccBuildProperties.SetPath("preprocessed_file_path", targetFilePath)

includesStrings := f.Map(includes.AsStrings(), utils.WrapWithHyphenI)
includesStrings := f.Map(includes.AsStrings(), cpp.WrapWithHyphenI)
gccBuildProperties.Set("includes", strings.Join(includesStrings, " "))

const gccPreprocRecipeProperty = "recipe.preproc.macros"
Expand Down
3 changes: 0 additions & 3 deletions arduino/builder/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,3 @@ func FindFilesInFolder(dir *paths.Path, recurse bool, extensions ...string) (pat
return dir.ReadDir(fileFilter)
}

func WrapWithHyphenI(value string) string {
return "\"-I" + value + "\""
}
10 changes: 10 additions & 0 deletions executils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func (p *Process) SetDir(dir string) {
p.cmd.Dir = dir
}

// GetDir gets the working directory of the command.
func (p *Process) GetDir() string {
return p.cmd.Dir
}

// SetDirFromPath sets the working directory of the command. If path is nil, Run
// runs the command in the calling process's current directory.
func (p *Process) SetDirFromPath(path *paths.Path) {
Expand Down Expand Up @@ -187,3 +192,8 @@ func (p *Process) RunAndCaptureOutput(ctx context.Context) ([]byte, []byte, erro
err := p.RunWithinContext(ctx)
return stdout.Bytes(), stderr.Bytes(), err
}

// GetArgs returns the command arguments
func (p *Process) GetArgs() []string {
return p.cmd.Args
}
26 changes: 16 additions & 10 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package builder_utils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"

bUtils "github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
Expand Down Expand Up @@ -173,7 +173,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
return nil, errors.WithStack(err)
}

command, err := PrepareCommandForRecipe(properties, recipe, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := PrepareCommandForRecipe(properties, recipe, false)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
properties.SetPath(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, archiveFilePath)
properties.SetPath(constants.BUILD_PROPERTIES_OBJECT_FILE, objectFile)

command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -260,7 +260,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile

const COMMANDLINE_LIMIT = 30000

func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool, toolEnv []string) (*exec.Cmd, error) {
func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool) (*executils.Process, error) {
pattern := buildProperties.Get(recipe)
if pattern == "" {
return nil, errors.Errorf(tr("%[1]s pattern is missing"), recipe)
Expand All @@ -275,24 +275,30 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
if err != nil {
return nil, errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)
command.Env = append(os.Environ(), toolEnv...)

// if the overall commandline is too long for the platform
// try reducing the length by making the filenames relative
// and changing working directory to build.path
var relativePath string
if len(commandLine) > COMMANDLINE_LIMIT {
relativePath := buildProperties.Get("build.path")
for i, arg := range command.Args {
relativePath = buildProperties.Get("build.path")
for i, arg := range parts {
if _, err := os.Stat(arg); os.IsNotExist(err) {
continue
}
rel, err := filepath.Rel(relativePath, arg)
if err == nil && !strings.Contains(rel, "..") && len(rel) < len(arg) {
command.Args[i] = rel
parts[i] = rel
}
}
command.Dir = relativePath
}

command, err := executils.NewProcess(nil, parts...)
if err != nil {
return nil, errors.WithStack(err)
}
if relativePath != "" {
command.SetDir(relativePath)
}

return command, nil
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs
return target
}

command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true)

for _, arg := range command.Args {
for _, arg := range command.GetArgs() {
if strings.HasPrefix(arg, "-D") {
*defines = appendIfNotPresent(*defines, arg)
continue
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"os"
"strings"

"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/buildcache"
"github.com/arduino/arduino-cli/i18n"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
Expand Down Expand Up @@ -80,7 +80,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
if variantFolder != nil && variantFolder.IsDir() {
includes = append(includes, variantFolder.String())
}
includes = f.Map(includes, utils.WrapWithHyphenI)
includes = f.Map(includes, cpp.WrapWithHyphenI)

var err error

Expand Down
12 changes: 6 additions & 6 deletions legacy/builder/phases/libraries_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package phases
import (
"strings"

"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/libraries"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
Expand All @@ -38,7 +38,7 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error {
librariesBuildPath := ctx.LibrariesBuildPath
buildProperties := ctx.BuildProperties
includesFolders := ctx.SketchLibrariesDetector.IncludeFolders()
includes := f.Map(includesFolders.AsStrings(), utils.WrapWithHyphenI)
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)
libs := ctx.SketchLibrariesDetector.ImportedLibraries()

if err := librariesBuildPath.MkdirAll(); err != nil {
Expand Down Expand Up @@ -67,9 +67,9 @@ func findExpectedPrecompiledLibFolder(ctx *types.Context, library *libraries.Lib
// Add fpu specifications if they exist
// To do so, resolve recipe.cpp.o.pattern,
// search for -mfpu=xxx -mfloat-abi=yyy and add to a subfolder
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, "recipe.cpp.o.pattern", true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, "recipe.cpp.o.pattern", true)
fpuSpecs := ""
for _, el := range strings.Split(command.String(), " ") {
for _, el := range command.GetArgs() {
if strings.Contains(el, FPU_CFLAG) {
toAdd := strings.Split(el, "=")
if len(toAdd) > 1 {
Expand All @@ -78,7 +78,7 @@ func findExpectedPrecompiledLibFolder(ctx *types.Context, library *libraries.Lib
}
}
}
for _, el := range strings.Split(command.String(), " ") {
for _, el := range command.GetArgs() {
if strings.Contains(el, FLOAT_ABI_CFLAG) {
toAdd := strings.Split(el, "=")
if len(toAdd) > 1 {
Expand Down Expand Up @@ -201,7 +201,7 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
}
} else {
if library.UtilityDir != nil {
includes = append(includes, utils.WrapWithHyphenI(library.UtilityDir.String()))
includes = append(includes, cpp.WrapWithHyphenI(library.UtilityDir.String()))
}
libObjectFiles, err := builder_utils.CompileFiles(ctx, library.SourceDir, libraryBuildPath, buildProperties, includes)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions legacy/builder/phases/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
properties.SetPath("archive_file_path", archive)
properties.SetPath("object_file", object)

command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -114,10 +114,10 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
properties.Set(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, coreArchiveFilePath.String())
properties.Set("object_files", objectFileList)

command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false)
if err != nil {
return err
}
}

_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
return err
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/phases/sizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Sizer) Run(ctx *types.Context) error {
}

func checkSizeAdvanced(ctx *types.Context, properties *properties.Map) error {
command, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.advanced_size.pattern", false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.advanced_size.pattern", false)
if err != nil {
return errors.New(tr("Error while determining sketch size: %s", err))
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
}

func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
command, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false)
if err != nil {
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
return
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/phases/sketch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package phases

import (
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
"github.com/arduino/arduino-cli/legacy/builder/types"
Expand All @@ -29,7 +29,7 @@ func (s *SketchBuilder) Run(ctx *types.Context) error {
sketchBuildPath := ctx.SketchBuildPath
buildProperties := ctx.BuildProperties
includesFolders := ctx.SketchLibrariesDetector.IncludeFolders()
includes := f.Map(includesFolders.AsStrings(), utils.WrapWithHyphenI)
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)

if err := sketchBuildPath.MkdirAll(); err != nil {
return errors.WithStack(err)
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/recipe_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
for _, recipe := range recipes {
logrus.Debugf(fmt.Sprintf("Running recipe: %s", recipe))

command, err := builder_utils.PrepareCommandForRecipe(properties, recipe, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
command, err := builder_utils.PrepareCommandForRecipe(properties, recipe, false)
if err != nil {
return errors.WithStack(err)
}

if ctx.OnlyUpdateCompilationDatabase && s.SkipIfOnlyUpdatingCompilationDatabase {
if ctx.Verbose {
ctx.Info(tr("Skipping: %[1]s", strings.Join(command.Args, " ")))
ctx.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " ")))
}
return nil
}
Expand Down
Loading

0 comments on commit a64582c

Please sign in to comment.