Skip to content

Commit 83abde2

Browse files
authored
Binaries export must now be explicitly specified (#1042)
1 parent f1ca408 commit 83abde2

File tree

11 files changed

+319
-148
lines changed

11 files changed

+319
-148
lines changed

Diff for: cli/compile/compile.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ var (
4747
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
4848
verify bool // Upload, verify uploaded binary after the upload.
4949
exportDir string // The compiled binary is written to this file
50-
dryRun bool // Use this flag to now write the output file
5150
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
5251
optimizeForDebug bool // Optimize compile output for debug, not for release
5352
programmer string // Use the specified programmer to upload
5453
clean bool // Cleanup the build folder and do not use any cached build
54+
exportBinaries bool // Copies compiled binaries to sketch folder when true
5555
)
5656

5757
// NewCommand created a new `compile` command
@@ -70,7 +70,6 @@ func NewCommand() *cobra.Command {
7070
command.Flags().BoolVar(&preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
7171
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", "Builds of 'core.a' are saved into this path to be cached and reused.")
7272
command.Flags().StringVarP(&exportDir, "output-dir", "", "", "Save build artifacts in this directory.")
73-
command.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform the build but do not copy the compile output file.")
7473
command.Flags().StringVar(&buildPath, "build-path", "",
7574
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
7675
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
@@ -88,6 +87,9 @@ func NewCommand() *cobra.Command {
8887
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
8988
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
9089
command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.")
90+
command.Flags().BoolVarP(&exportBinaries, "export-binaries", "e", false, "If set built binaries will be exported to the sketch folder.")
91+
92+
configuration.Settings.BindPFlag("sketch.always_export_binaries", command.Flags().Lookup("export-binaries"))
9193

9294
return command
9395
}
@@ -120,10 +122,10 @@ func run(cmd *cobra.Command, args []string) {
120122
Quiet: quiet,
121123
VidPid: vidPid,
122124
ExportDir: exportDir,
123-
DryRun: dryRun,
124125
Libraries: libraries,
125126
OptimizeForDebug: optimizeForDebug,
126127
Clean: clean,
128+
ExportBinaries: exportBinaries,
127129
}, os.Stdout, os.Stderr, configuration.Settings.GetString("logging.level") == "debug")
128130

129131
if err != nil {
@@ -139,7 +141,7 @@ func run(cmd *cobra.Command, args []string) {
139141
Port: port,
140142
Verbose: verbose,
141143
Verify: verify,
142-
ImportDir: exportDir,
144+
ImportDir: buildPath,
143145
Programmer: programmer,
144146
}, os.Stdout, os.Stderr)
145147

Diff for: commands/compile/compile.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525
"strings"
2626

27+
bldr "github.com/arduino/arduino-cli/arduino/builder"
2728
"github.com/arduino/arduino-cli/arduino/cores"
2829
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2930
"github.com/arduino/arduino-cli/arduino/sketches"
@@ -54,15 +55,11 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
5455
"verbose": strconv.FormatBool(req.Verbose),
5556
"quiet": strconv.FormatBool(req.Quiet),
5657
"vidPid": req.VidPid,
57-
"exportFile": telemetry.Sanitize(req.ExportFile), // deprecated
5858
"exportDir": telemetry.Sanitize(req.GetExportDir()),
5959
"jobs": strconv.FormatInt(int64(req.Jobs), 10),
6060
"libraries": strings.Join(req.Libraries, ","),
6161
"clean": strconv.FormatBool(req.GetClean()),
62-
}
63-
64-
if req.GetExportFile() != "" {
65-
outStream.Write([]byte(fmt.Sprintln("Compile.ExportFile has been deprecated. The ExportFile parameter will be ignored, use ExportDir instead.")))
62+
"exportBinaries": strconv.FormatBool(req.GetExportBinaries()),
6663
}
6764

6865
// Use defer func() to evaluate tags map when function returns
@@ -127,12 +124,14 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
127124
builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...)
128125
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
129126

130-
if req.GetBuildPath() != "" {
127+
if req.GetBuildPath() == "" {
128+
builderCtx.BuildPath = bldr.GenBuildPath(sketch.FullPath)
129+
} else {
131130
builderCtx.BuildPath = paths.New(req.GetBuildPath())
132-
err = builderCtx.BuildPath.MkdirAll()
133-
if err != nil {
134-
return nil, fmt.Errorf("cannot create build directory: %s", err)
135-
}
131+
}
132+
133+
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
134+
return nil, fmt.Errorf("cannot create build directory: %s", err)
136135
}
137136

138137
builderCtx.Verbose = req.GetVerbose()
@@ -202,7 +201,8 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
202201
return nil, err
203202
}
204203

205-
if !req.GetDryRun() {
204+
// If the export directory is set we assume you want to export the binaries
205+
if req.GetExportBinaries() || req.GetExportDir() != "" {
206206
var exportPath *paths.Path
207207
if exportDir := req.GetExportDir(); exportDir != "" {
208208
exportPath = paths.New(exportDir)

Diff for: commands/upload/upload.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26+
bldr "github.com/arduino/arduino-cli/arduino/builder"
2627
"github.com/arduino/arduino-cli/arduino/cores"
2728
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2829
"github.com/arduino/arduino-cli/arduino/serialutils"
@@ -446,16 +447,9 @@ func determineBuildPathAndSketchName(importFile, importDir string, sketch *sketc
446447
return nil, "", fmt.Errorf("no sketch or build directory/file specified")
447448
}
448449

449-
// Case 4: only sketch specified. In this case we use the default sketch build path
450+
// Case 4: only sketch specified. In this case we use the generated build path
450451
// and the given sketch name.
451-
452-
// TODO: Create a function to obtain importPath from sketch
453-
// Add FQBN (without configs part) to export path
454-
if fqbn == nil {
455-
return nil, "", fmt.Errorf("missing FQBN")
456-
}
457-
fqbnSuffix := strings.Replace(fqbn.StringWithoutConfig(), ":", ".", -1)
458-
return sketch.FullPath.Join("build").Join(fqbnSuffix), sketch.Name + ".ino", nil
452+
return bldr.GenBuildPath(sketch.FullPath), sketch.Name + ".ino", nil
459453
}
460454

461455
func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {

Diff for: commands/upload/upload_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"testing"
2323

24+
"github.com/arduino/arduino-cli/arduino/builder"
2425
"github.com/arduino/arduino-cli/arduino/cores"
2526
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2627
"github.com/arduino/arduino-cli/arduino/sketches"
@@ -76,15 +77,14 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
7677
{"", "testdata/build_path_2", nil, nil, "testdata/build_path_2", "Blink.ino"},
7778
// 03: error: used both importPath and importFile
7879
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", ""},
79-
// 04: error: only sketch without FQBN
80-
{"", "", blonk, nil, "<nil>", ""},
80+
// 04: only sketch without FQBN
81+
{"", "", blonk, nil, builder.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
8182
// 05: use importFile to detect build.path and project_name, sketch is ignored.
8283
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8384
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
8485
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8586
// 07: error: used both importPath and importFile
8687
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", ""},
87-
8888
// 08: error: no data passed in
8989
{"", "", nil, fqbn, "<nil>", ""},
9090
// 09: use importFile to detect build.path and project_name, fqbn ignored
@@ -94,14 +94,13 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
9494
// 11: error: used both importPath and importFile
9595
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", ""},
9696
// 12: use sketch to determine project name and sketch+fqbn to determine build path
97-
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino"},
97+
{"", "", blonk, fqbn, builder.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
9898
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
9999
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
100100
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn
101101
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
102102
// 15: error: used both importPath and importFile
103103
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", ""},
104-
105104
// 16: importPath containing multiple firmwares, but one has the same name as the containing folder
106105
{"", "testdata/firmware", nil, fqbn, "testdata/firmware", "firmware.ino"},
107106
// 17: importFile among multiple firmwares

Diff for: configuration/defaults.go

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func SetDefaults(settings *viper.Viper) {
3636
settings.SetDefault("directories.Downloads", filepath.Join(getDefaultArduinoDataDir(), "staging"))
3737
settings.SetDefault("directories.User", getDefaultUserDir())
3838

39+
// Sketch compilation
40+
settings.SetDefault("sketch.always_export_binaries", false)
41+
3942
// daemon settings
4043
settings.SetDefault("daemon.port", "50051")
4144

@@ -52,4 +55,5 @@ func SetDefaults(settings *viper.Viper) {
5255
settings.BindEnv("directories.User", "ARDUINO_SKETCHBOOK_DIR")
5356
settings.BindEnv("directories.Downloads", "ARDUINO_DOWNLOADS_DIR")
5457
settings.BindEnv("directories.Data", "ARDUINO_DATA_DIR")
58+
settings.BindEnv("sketch.always_export_binaries", "ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES")
5559
}

Diff for: legacy/builder/builder.go

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ const DEFAULT_SOFTWARE = "ARDUINO"
4242
type Builder struct{}
4343

4444
func (s *Builder) Run(ctx *types.Context) error {
45-
if ctx.BuildPath == nil {
46-
ctx.BuildPath = bldr.GenBuildPath(ctx.SketchLocation)
47-
}
48-
4945
if err := bldr.EnsureBuildPathExists(ctx.BuildPath.String()); err != nil {
5046
return err
5147
}

0 commit comments

Comments
 (0)