Skip to content

Commit

Permalink
Propagate 'user-agent' info to tools via environment vars
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jan 27, 2022
1 parent 5124948 commit eb7c456
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion arduino/cores/packagemanager/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (pm *PackageManager) RunPostInstallScript(platformRelease *cores.PlatformRe
}
postInstall := platformRelease.InstallDir.Join(postInstallFilename)
if postInstall.Exist() && postInstall.IsNotDir() {
cmd, err := executils.NewProcessFromPath(nil, postInstall)
cmd, err := executils.NewProcessFromPath(pm.GetEnvVarsForSpawnedProcess(), postInstall)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func NewPackageManager(indexDir, packagesDir, downloadDir, tempDir *paths.Path,
}
}

// GetEnvVarsForSpawnedProcess produces a set of environment variables that
// must be set to all processes spawned from the arduino-cli.
func (pm *PackageManager) GetEnvVarsForSpawnedProcess() []string {
return []string{
"ARDUINO_USER_AGENT=" + pm.userAgent,
}
}

// Clear resets the PackageManager to its initial state
func (pm *PackageManager) Clear() {
pm.Packages = cores.NewPackages()
Expand Down
2 changes: 1 addition & 1 deletion commands/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader,
}
entry.Debug("Executing debugger")

cmd, err := executils.NewProcess(nil, commandLine...)
cmd, err := executils.NewProcess(pm.GetEnvVarsForSpawnedProcess(), commandLine...)
if err != nil {
return nil, &arduino.FailedDebugError{Message: tr("Cannot execute debug tool"), Cause: err}
}
Expand Down
13 changes: 7 additions & 6 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,20 @@ func runProgramAction(pm *packagemanager.PackageManager,
}

// Run recipes for upload
toolEnv := pm.GetEnvVarsForSpawnedProcess()
if burnBootloader {
if err := runTool("erase.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
if err := runTool("erase.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
return &arduino.FailedUploadError{Message: tr("Failed chip erase"), Cause: err}
}
if err := runTool("bootloader.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
if err := runTool("bootloader.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
return &arduino.FailedUploadError{Message: tr("Failed to burn bootloader"), Cause: err}
}
} else if programmer != nil {
if err := runTool("program.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
if err := runTool("program.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
return &arduino.FailedUploadError{Message: tr("Failed programming"), Cause: err}
}
} else {
if err := runTool("upload.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
if err := runTool("upload.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
return &arduino.FailedUploadError{Message: tr("Failed uploading"), Cause: err}
}
}
Expand All @@ -552,7 +553,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
return nil
}

func runTool(recipeID string, props *properties.Map, outStream, errStream io.Writer, verbose bool, dryRun bool) error {
func runTool(recipeID string, props *properties.Map, outStream, errStream io.Writer, verbose bool, dryRun bool, toolEnv []string) error {
recipe, ok := props.GetOk(recipeID)
if !ok {
return fmt.Errorf(tr("recipe not found '%s'"), recipeID)
Expand All @@ -577,7 +578,7 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
if dryRun {
return nil
}
cmd, err := executils.NewProcess(nil, cmdArgs...)
cmd, err := executils.NewProcess(toolEnv, cmdArgs...)
if err != nil {
return fmt.Errorf(tr("cannot execute upload tool: %s"), err)
}
Expand Down
8 changes: 5 additions & 3 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
if err != nil {
return nil, errors.WithStack(err)
}
command, err := PrepareCommandForRecipe(properties, recipe, false)
command, err := PrepareCommandForRecipe(properties, recipe, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down Expand Up @@ -469,7 +469,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)
command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -485,7 +485,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile

const COMMANDLINE_LIMIT = 30000

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

// if the overall commandline is too long for the platform
// try reducing the length by making the filenames relative
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func canExportCmakeProject(ctx *types.Context) bool {
}

func extractCompileFlags(ctx *types.Context, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) {
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true)
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, recipe, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())

for _, arg := range command.Args {
if strings.HasPrefix(arg, "-D") {
Expand Down
4 changes: 4 additions & 0 deletions legacy/builder/ctags_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package builder

import (
"os"
"os/exec"

"github.com/arduino/arduino-cli/legacy/builder/constants"
Expand Down Expand Up @@ -47,6 +48,9 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
return errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)
command.Env = append(command.Env, os.Environ()...)
command.Env = append(command.Env, ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)

sourceBytes, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Ignore /* stderr */)
if err != nil {
return errors.WithStack(err)
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/gcc_preproc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths
properties.Set(constants.RECIPE_PREPROC_MACROS, GeneratePreprocPatternFromCompile(properties.Get(constants.RECIPE_CPP_PATTERN)))
}

cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true)
cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/libraries_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ 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, constants.RECIPE_CPP_PATTERN, true)
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, constants.RECIPE_CPP_PATTERN, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
fpuSpecs := ""
for _, el := range strings.Split(command.String(), " ") {
if strings.Contains(el, FPU_CFLAG) {
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/phases/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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)
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -113,7 +113,7 @@ 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)
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/sizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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, constants.RECIPE_SIZE_PATTERN, false)
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_SIZE_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
return
Expand Down
3 changes: 3 additions & 0 deletions legacy/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package builder

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -109,6 +110,8 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
return errors.WithStack(err)
}
command := exec.Command(parts[0], parts[1:]...)
command.Env = append(command.Env, os.Environ()...)
command.Env = append(command.Env, ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)

if runtime.GOOS == "windows" {
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/recipe_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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)
command, err := builder_utils.PrepareCommandForRecipe(properties, recipe, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
if err != nil {
return errors.WithStack(err)
}
Expand Down

0 comments on commit eb7c456

Please sign in to comment.