Skip to content

Some small refactoring on legacy package (part 2) #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Changes from all commits
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
36 changes: 18 additions & 18 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
@@ -248,7 +248,12 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
return nil, errors.WithStack(err)
}
if !objIsUpToDate {
_, _, _, err := ExecRecipe(ctx, properties, recipe, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
command, err := PrepareCommandForRecipe(properties, recipe, false)
if err != nil {
return nil, errors.WithStack(err)
}

_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if err != nil {
return nil, errors.WithStack(err)
}
@@ -479,23 +484,18 @@ 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)

if _, _, _, err := ExecRecipe(ctx, properties, constants.RECIPE_AR_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
if err != nil {
return nil, errors.WithStack(err)
}
}

return archiveFilePath, nil
}

func ExecRecipe(ctx *types.Context, buildProperties *properties.Map, recipe string, stdout int, stderr int) (*exec.Cmd, []byte, []byte, error) {
// See util.ExecCommand for stdout/stderr arguments
command, err := PrepareCommandForRecipe(buildProperties, recipe, false)
if err != nil {
return nil, nil, nil, errors.WithStack(err)
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if err != nil {
return nil, errors.WithStack(err)
}
}

outbytes, errbytes, err := utils.ExecCommand(ctx, command, stdout, stderr)
return command, outbytes, errbytes, err
return archiveFilePath, nil
}

const COMMANDLINE_LIMIT = 30000
@@ -511,7 +511,11 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
}

command, err := utils.PrepareCommand(commandLine)
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return nil, errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)

// if the overall commandline is too long for the platform
// try reducing the length by making the filenames relative
@@ -530,9 +534,5 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
command.Dir = relativePath
}

if err != nil {
return nil, errors.WithStack(err)
}

return command, nil
}
17 changes: 10 additions & 7 deletions legacy/builder/ctags_runner.go
Original file line number Diff line number Diff line change
@@ -16,10 +16,13 @@
package builder

import (
"os/exec"

"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/ctags"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/arduino-cli/legacy/builder/utils"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

@@ -29,21 +32,21 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
buildProperties := ctx.BuildProperties
ctagsTargetFilePath := ctx.CTagsTargetFile

properties := buildProperties.Clone()
properties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)
ctagsProperties := buildProperties.Clone()
ctagsProperties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
ctagsProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, ctagsTargetFilePath)

pattern := properties.Get(constants.BUILD_PROPERTIES_PATTERN)
pattern := ctagsProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
if pattern == constants.EMPTY_STRING {
return errors.Errorf("%s pattern is missing", constants.CTAGS)
}

commandLine := properties.ExpandPropsInString(pattern)
command, err := utils.PrepareCommand(commandLine)
commandLine := ctagsProperties.ExpandPropsInString(pattern)
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return errors.WithStack(err)
}

command := exec.Command(parts[0], parts[1:]...)
sourceBytes, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Ignore /* stderr */)
if err != nil {
return errors.WithStack(err)
16 changes: 13 additions & 3 deletions legacy/builder/phases/linker.go
Original file line number Diff line number Diff line change
@@ -80,9 +80,14 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
properties.Set("archive_file", archive.Base())
properties.SetPath("archive_file_path", archive)
properties.SetPath("object_file", object)
_, _, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_AR_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)

command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
if err != nil {
return err
return errors.WithStack(err)
}

if _, _, err := utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
return errors.WithStack(err)
}
}

@@ -97,7 +102,12 @@ 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)

_, _, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_C_COMBINE_PATTERN, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
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
}

8 changes: 7 additions & 1 deletion legacy/builder/phases/sizer.go
Original file line number Diff line number Diff line change
@@ -127,7 +127,13 @@ 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) {
_, out, _, err := builder_utils.ExecRecipe(ctx, properties, constants.RECIPE_SIZE_PATTERN, utils.Capture /* stdout */, utils.Show /* stderr */)
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_SIZE_PATTERN, false)
if err != nil {
resErr = errors.New("Error while determining sketch size: " + err.Error())
return
}

out, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Show /* stderr */)
if err != nil {
resErr = errors.New("Error while determining sketch size: " + err.Error())
return
17 changes: 9 additions & 8 deletions legacy/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
@@ -78,10 +78,10 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
buildProperties := ctx.BuildProperties
targetFilePath := ctx.PreprocPath.Join(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E)

properties := buildProperties.Clone()
preprocProperties := buildProperties.Clone()
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
properties.Merge(toolProps)
properties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
preprocProperties.Merge(toolProps)
preprocProperties.SetPath(constants.BUILD_PROPERTIES_SOURCE_FILE, targetFilePath)
if ctx.CodeCompleteAt != "" {
if runtime.GOOS == "windows" {
//use relative filepath to avoid ":" escaping
@@ -93,21 +93,22 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
ctx.CodeCompleteAt = strings.Join(splt[1:], ":")
}
}
properties.Set("codecomplete", "-output-code-completions="+ctx.CodeCompleteAt)
preprocProperties.Set("codecomplete", "-output-code-completions="+ctx.CodeCompleteAt)
} else {
properties.Set("codecomplete", "")
preprocProperties.Set("codecomplete", "")
}

pattern := properties.Get(constants.BUILD_PROPERTIES_PATTERN)
pattern := preprocProperties.Get(constants.BUILD_PROPERTIES_PATTERN)
if pattern == constants.EMPTY_STRING {
return errors.New("arduino-preprocessor pattern is missing")
}

commandLine := properties.ExpandPropsInString(pattern)
command, err := utils.PrepareCommand(commandLine)
commandLine := preprocProperties.ExpandPropsInString(pattern)
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)

if runtime.GOOS == "windows" {
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
8 changes: 7 additions & 1 deletion legacy/builder/recipe_runner.go
Original file line number Diff line number Diff line change
@@ -47,7 +47,13 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
if ctx.DebugLevel >= 10 {
logger.Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_RECIPE, recipe)
}
_, _, _, err := builder_utils.ExecRecipe(ctx, properties, recipe, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)

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

_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if err != nil {
return errors.WithStack(err)
}
9 changes: 0 additions & 9 deletions legacy/builder/utils/utils.go
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ import (
"github.com/arduino/arduino-cli/legacy/builder/gohasissues"
"github.com/arduino/arduino-cli/legacy/builder/types"
paths "github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
@@ -147,14 +146,6 @@ func TrimSpace(value string) string {
return strings.TrimSpace(value)
}

func PrepareCommand(pattern string) (*exec.Cmd, error) {
parts, err := properties.SplitQuotedString(pattern, `"'`, false)
if err != nil {
return nil, errors.WithStack(err)
}
return exec.Command(parts[0], parts[1:]...), nil
}

func printableArgument(arg string) string {
if strings.ContainsAny(arg, "\"\\ \t") {
arg = strings.Replace(arg, "\\", "\\\\", -1)