Skip to content

Commit eb7c456

Browse files
committed
Propagate 'user-agent' info to tools via environment vars
1 parent 5124948 commit eb7c456

File tree

13 files changed

+36
-18
lines changed

13 files changed

+36
-18
lines changed

Diff for: arduino/cores/packagemanager/install_uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (pm *PackageManager) RunPostInstallScript(platformRelease *cores.PlatformRe
7070
}
7171
postInstall := platformRelease.InstallDir.Join(postInstallFilename)
7272
if postInstall.Exist() && postInstall.IsNotDir() {
73-
cmd, err := executils.NewProcessFromPath(nil, postInstall)
73+
cmd, err := executils.NewProcessFromPath(pm.GetEnvVarsForSpawnedProcess(), postInstall)
7474
if err != nil {
7575
return err
7676
}

Diff for: arduino/cores/packagemanager/package_manager.go

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func NewPackageManager(indexDir, packagesDir, downloadDir, tempDir *paths.Path,
6565
}
6666
}
6767

68+
// GetEnvVarsForSpawnedProcess produces a set of environment variables that
69+
// must be set to all processes spawned from the arduino-cli.
70+
func (pm *PackageManager) GetEnvVarsForSpawnedProcess() []string {
71+
return []string{
72+
"ARDUINO_USER_AGENT=" + pm.userAgent,
73+
}
74+
}
75+
6876
// Clear resets the PackageManager to its initial state
6977
func (pm *PackageManager) Clear() {
7078
pm.Packages = cores.NewPackages()

Diff for: commands/debug/debug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader,
6262
}
6363
entry.Debug("Executing debugger")
6464

65-
cmd, err := executils.NewProcess(nil, commandLine...)
65+
cmd, err := executils.NewProcess(pm.GetEnvVarsForSpawnedProcess(), commandLine...)
6666
if err != nil {
6767
return nil, &arduino.FailedDebugError{Message: tr("Cannot execute debug tool"), Cause: err}
6868
}

Diff for: commands/upload/upload.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -531,19 +531,20 @@ func runProgramAction(pm *packagemanager.PackageManager,
531531
}
532532

533533
// Run recipes for upload
534+
toolEnv := pm.GetEnvVarsForSpawnedProcess()
534535
if burnBootloader {
535-
if err := runTool("erase.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
536+
if err := runTool("erase.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
536537
return &arduino.FailedUploadError{Message: tr("Failed chip erase"), Cause: err}
537538
}
538-
if err := runTool("bootloader.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
539+
if err := runTool("bootloader.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
539540
return &arduino.FailedUploadError{Message: tr("Failed to burn bootloader"), Cause: err}
540541
}
541542
} else if programmer != nil {
542-
if err := runTool("program.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
543+
if err := runTool("program.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
543544
return &arduino.FailedUploadError{Message: tr("Failed programming"), Cause: err}
544545
}
545546
} else {
546-
if err := runTool("upload.pattern", uploadProperties, outStream, errStream, verbose, dryRun); err != nil {
547+
if err := runTool("upload.pattern", uploadProperties, outStream, errStream, verbose, dryRun, toolEnv); err != nil {
547548
return &arduino.FailedUploadError{Message: tr("Failed uploading"), Cause: err}
548549
}
549550
}
@@ -552,7 +553,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
552553
return nil
553554
}
554555

555-
func runTool(recipeID string, props *properties.Map, outStream, errStream io.Writer, verbose bool, dryRun bool) error {
556+
func runTool(recipeID string, props *properties.Map, outStream, errStream io.Writer, verbose bool, dryRun bool, toolEnv []string) error {
556557
recipe, ok := props.GetOk(recipeID)
557558
if !ok {
558559
return fmt.Errorf(tr("recipe not found '%s'"), recipeID)
@@ -577,7 +578,7 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
577578
if dryRun {
578579
return nil
579580
}
580-
cmd, err := executils.NewProcess(nil, cmdArgs...)
581+
cmd, err := executils.NewProcess(toolEnv, cmdArgs...)
581582
if err != nil {
582583
return fmt.Errorf(tr("cannot execute upload tool: %s"), err)
583584
}

Diff for: legacy/builder/builder_utils/utils.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
237237
if err != nil {
238238
return nil, errors.WithStack(err)
239239
}
240-
command, err := PrepareCommandForRecipe(properties, recipe, false)
240+
command, err := PrepareCommandForRecipe(properties, recipe, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
241241
if err != nil {
242242
return nil, errors.WithStack(err)
243243
}
@@ -469,7 +469,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
469469
properties.SetPath(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, archiveFilePath)
470470
properties.SetPath(constants.BUILD_PROPERTIES_OBJECT_FILE, objectFile)
471471

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

486486
const COMMANDLINE_LIMIT = 30000
487487

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

505507
// if the overall commandline is too long for the platform
506508
// try reducing the length by making the filenames relative

Diff for: legacy/builder/create_cmake_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func canExportCmakeProject(ctx *types.Context) bool {
237237
}
238238

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

242242
for _, arg := range command.Args {
243243
if strings.HasPrefix(arg, "-D") {

Diff for: legacy/builder/ctags_runner.go

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package builder
1717

1818
import (
19+
"os"
1920
"os/exec"
2021

2122
"github.com/arduino/arduino-cli/legacy/builder/constants"
@@ -47,6 +48,9 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
4748
return errors.WithStack(err)
4849
}
4950
command := exec.Command(parts[0], parts[1:]...)
51+
command.Env = append(command.Env, os.Environ()...)
52+
command.Env = append(command.Env, ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)
53+
5054
sourceBytes, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Ignore /* stderr */)
5155
if err != nil {
5256
return errors.WithStack(err)

Diff for: legacy/builder/gcc_preproc_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths
6969
properties.Set(constants.RECIPE_PREPROC_MACROS, GeneratePreprocPatternFromCompile(properties.Get(constants.RECIPE_CPP_PATTERN)))
7070
}
7171

72-
cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true)
72+
cmd, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_PREPROC_MACROS, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
7373
if err != nil {
7474
return nil, errors.WithStack(err)
7575
}

Diff for: legacy/builder/phases/libraries_builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func findExpectedPrecompiledLibFolder(ctx *types.Context, library *libraries.Lib
6565
// Add fpu specifications if they exist
6666
// To do so, resolve recipe.cpp.o.pattern,
6767
// search for -mfpu=xxx -mfloat-abi=yyy and add to a subfolder
68-
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, constants.RECIPE_CPP_PATTERN, true)
68+
command, _ := builder_utils.PrepareCommandForRecipe(ctx.BuildProperties, constants.RECIPE_CPP_PATTERN, true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
6969
fpuSpecs := ""
7070
for _, el := range strings.Split(command.String(), " ") {
7171
if strings.Contains(el, FPU_CFLAG) {

Diff for: legacy/builder/phases/linker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
9292
properties.SetPath("archive_file_path", archive)
9393
properties.SetPath("object_file", object)
9494

95-
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
95+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
9696
if err != nil {
9797
return errors.WithStack(err)
9898
}
@@ -113,7 +113,7 @@ func link(ctx *types.Context, objectFiles paths.PathList, coreDotARelPath *paths
113113
properties.Set(constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH, coreArchiveFilePath.String())
114114
properties.Set("object_files", objectFileList)
115115

116-
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false)
116+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
117117
if err != nil {
118118
return err
119119
}

Diff for: legacy/builder/phases/sizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
135135
}
136136

137137
func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
138-
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_SIZE_PATTERN, false)
138+
command, err := builder_utils.PrepareCommandForRecipe(properties, constants.RECIPE_SIZE_PATTERN, false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
139139
if err != nil {
140140
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
141141
return

Diff for: legacy/builder/preprocess_sketch.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package builder
1717

1818
import (
1919
"fmt"
20+
"os"
2021
"os/exec"
2122
"path/filepath"
2223
"runtime"
@@ -109,6 +110,8 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
109110
return errors.WithStack(err)
110111
}
111112
command := exec.Command(parts[0], parts[1:]...)
113+
command.Env = append(command.Env, os.Environ()...)
114+
command.Env = append(command.Env, ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)
112115

113116
if runtime.GOOS == "windows" {
114117
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)

Diff for: legacy/builder/recipe_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *RecipeByPrefixSuffixRunner) Run(ctx *types.Context) error {
4444
for _, recipe := range recipes {
4545
logrus.Debugf(fmt.Sprintf("Running recipe: %s", recipe))
4646

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

0 commit comments

Comments
 (0)