From 4ad4cf168cc7b5c3154644ce2c73586dcb7381da Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 11 Nov 2020 19:16:00 +0100 Subject: [PATCH 1/2] legacy: inlined utils.PrepareCommand --- legacy/builder/builder_utils/utils.go | 10 +++++----- legacy/builder/ctags_runner.go | 17 ++++++++++------- legacy/builder/preprocess_sketch.go | 17 +++++++++-------- legacy/builder/utils/utils.go | 9 --------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/legacy/builder/builder_utils/utils.go b/legacy/builder/builder_utils/utils.go index f74e50acdb8..67613451505 100644 --- a/legacy/builder/builder_utils/utils.go +++ b/legacy/builder/builder_utils/utils.go @@ -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 } diff --git a/legacy/builder/ctags_runner.go b/legacy/builder/ctags_runner.go index f0809265fe5..b132fc8ad03 100644 --- a/legacy/builder/ctags_runner.go +++ b/legacy/builder/ctags_runner.go @@ -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) diff --git a/legacy/builder/preprocess_sketch.go b/legacy/builder/preprocess_sketch.go index 48ad2e5d5f8..10360f09527 100644 --- a/legacy/builder/preprocess_sketch.go +++ b/legacy/builder/preprocess_sketch.go @@ -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) diff --git a/legacy/builder/utils/utils.go b/legacy/builder/utils/utils.go index 530fe0c751a..58fb161f5fc 100644 --- a/legacy/builder/utils/utils.go +++ b/legacy/builder/utils/utils.go @@ -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) From 5845da770f6d6b97e39e85e32e61f1660e8c0b9d Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 11 Nov 2020 20:04:12 +0100 Subject: [PATCH 2/2] legacy: inlined ExecRecipe function --- legacy/builder/builder_utils/utils.go | 26 +++++++++++++------------- legacy/builder/phases/linker.go | 16 +++++++++++++--- legacy/builder/phases/sizer.go | 8 +++++++- legacy/builder/recipe_runner.go | 8 +++++++- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/legacy/builder/builder_utils/utils.go b/legacy/builder/builder_utils/utils.go index 67613451505..7f1d098dea8 100644 --- a/legacy/builder/builder_utils/utils.go +++ b/legacy/builder/builder_utils/utils.go @@ -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 diff --git a/legacy/builder/phases/linker.go b/legacy/builder/phases/linker.go index 4df9fb850c7..0d3c7756e0b 100644 --- a/legacy/builder/phases/linker.go +++ b/legacy/builder/phases/linker.go @@ -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 } diff --git a/legacy/builder/phases/sizer.go b/legacy/builder/phases/sizer.go index a650ce68292..ad870163dcb 100644 --- a/legacy/builder/phases/sizer.go +++ b/legacy/builder/phases/sizer.go @@ -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 diff --git a/legacy/builder/recipe_runner.go b/legacy/builder/recipe_runner.go index 45a9e2d4e97..d7bb389b75f 100644 --- a/legacy/builder/recipe_runner.go +++ b/legacy/builder/recipe_runner.go @@ -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) }