Skip to content

Commit fbd35df

Browse files
cmaglieRoberto Sora
and
Roberto Sora
authored
Added dry-run mode in Compile command (#619)
* Added dry-run mode in Compile command * Update cli/compile/compile.go Co-Authored-By: Roberto Sora <[email protected]> Co-authored-by: Roberto Sora <[email protected]>
1 parent f855fea commit fbd35df

File tree

4 files changed

+90
-75
lines changed

4 files changed

+90
-75
lines changed

Diff for: cli/compile/compile.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var (
4747
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
4848
verify bool // Upload, verify uploaded binary after the upload.
4949
exportFile string // The compiled binary is written to this file
50+
dryRun bool // Use this flag to now write the output file
5051
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
5152
optimizeForDebug bool // Optimize compile output for debug, not for release
5253
)
@@ -67,6 +68,7 @@ func NewCommand() *cobra.Command {
6768
command.Flags().BoolVar(&preprocess, "preprocess", false, "Print preprocessed code to stdout instead of compiling.")
6869
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", "Builds of 'core.a' are saved into this path to be cached and reused.")
6970
command.Flags().StringVarP(&exportFile, "output", "o", "", "Filename of the compile output.")
71+
command.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform the build but do not copy the compile output file.")
7072
command.Flags().StringVar(&buildPath, "build-path", "",
7173
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
7274
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
@@ -114,6 +116,7 @@ func run(cmd *cobra.Command, args []string) {
114116
Quiet: quiet,
115117
VidPid: vidPid,
116118
ExportFile: exportFile,
119+
DryRun: dryRun,
117120
Libraries: libraries,
118121
OptimizeForDebug: optimizeForDebug,
119122
}, os.Stdout, os.Stderr, viper.GetString("logging.level") == "debug")

Diff for: commands/compile/compile.go

+50-48
Original file line numberDiff line numberDiff line change
@@ -196,59 +196,61 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
196196
return nil, err
197197
}
198198

199-
// FIXME: Make a function to obtain these info...
200-
outputPath := paths.New(
201-
builderCtx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")) // "/build/path/sketch.ino.bin"
202-
ext := outputPath.Ext() // ".hex" | ".bin"
203-
base := outputPath.Base() // "sketch.ino.hex"
204-
base = base[:len(base)-len(ext)] // "sketch.ino"
205-
206-
// FIXME: Make a function to produce a better name...
207-
// Make the filename without the FQBN configs part
208-
fqbn.Configs = properties.NewMap()
209-
fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)
210-
211-
var exportPath *paths.Path
212-
var exportFile string
213-
if req.GetExportFile() == "" {
214-
if sketch.FullPath.IsDir() {
215-
exportPath = sketch.FullPath
199+
if !req.GetDryRun() {
200+
// FIXME: Make a function to obtain these info...
201+
outputPath := paths.New(
202+
builderCtx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")) // "/build/path/sketch.ino.bin"
203+
ext := outputPath.Ext() // ".hex" | ".bin"
204+
base := outputPath.Base() // "sketch.ino.hex"
205+
base = base[:len(base)-len(ext)] // "sketch.ino"
206+
207+
// FIXME: Make a function to produce a better name...
208+
// Make the filename without the FQBN configs part
209+
fqbn.Configs = properties.NewMap()
210+
fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)
211+
212+
var exportPath *paths.Path
213+
var exportFile string
214+
if req.GetExportFile() == "" {
215+
if sketch.FullPath.IsDir() {
216+
exportPath = sketch.FullPath
217+
} else {
218+
exportPath = sketch.FullPath.Parent()
219+
}
220+
exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno"
216221
} else {
217-
exportPath = sketch.FullPath.Parent()
218-
}
219-
exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno"
220-
} else {
221-
exportPath = paths.New(req.GetExportFile()).Parent()
222-
exportFile = paths.New(req.GetExportFile()).Base()
223-
if strings.HasSuffix(exportFile, ext) {
224-
exportFile = exportFile[:len(exportFile)-len(ext)]
222+
exportPath = paths.New(req.GetExportFile()).Parent()
223+
exportFile = paths.New(req.GetExportFile()).Base()
224+
if strings.HasSuffix(exportFile, ext) {
225+
exportFile = exportFile[:len(exportFile)-len(ext)]
226+
}
225227
}
226-
}
227228

228-
// Copy "sketch.ino.*.hex" / "sketch.ino.*.bin" artifacts to sketch directory
229-
srcDir, err := outputPath.Parent().ReadDir() // read "/build/path/*"
230-
if err != nil {
231-
return nil, fmt.Errorf("reading build directory: %s", err)
232-
}
233-
srcDir.FilterPrefix(base + ".")
234-
srcDir.FilterSuffix(ext)
235-
for _, srcOutput := range srcDir {
236-
srcFilename := srcOutput.Base() // "sketch.ino.*.bin"
237-
srcFilename = srcFilename[len(base):] // ".*.bin"
238-
dstOutput := exportPath.Join(exportFile + srcFilename)
239-
logrus.WithField("from", srcOutput).WithField("to", dstOutput).Debug("copying sketch build output")
240-
if err = srcOutput.CopyTo(dstOutput); err != nil {
241-
return nil, fmt.Errorf("copying output file: %s", err)
229+
// Copy "sketch.ino.*.hex" / "sketch.ino.*.bin" artifacts to sketch directory
230+
srcDir, err := outputPath.Parent().ReadDir() // read "/build/path/*"
231+
if err != nil {
232+
return nil, fmt.Errorf("reading build directory: %s", err)
233+
}
234+
srcDir.FilterPrefix(base + ".")
235+
srcDir.FilterSuffix(ext)
236+
for _, srcOutput := range srcDir {
237+
srcFilename := srcOutput.Base() // "sketch.ino.*.bin"
238+
srcFilename = srcFilename[len(base):] // ".*.bin"
239+
dstOutput := exportPath.Join(exportFile + srcFilename)
240+
logrus.WithField("from", srcOutput).WithField("to", dstOutput).Debug("copying sketch build output")
241+
if err = srcOutput.CopyTo(dstOutput); err != nil {
242+
return nil, fmt.Errorf("copying output file: %s", err)
243+
}
242244
}
243-
}
244245

245-
// Copy .elf file to sketch directory
246-
srcElf := outputPath.Parent().Join(base + ".elf")
247-
if srcElf.Exist() {
248-
dstElf := exportPath.Join(exportFile + ".elf")
249-
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
250-
if err = srcElf.CopyTo(dstElf); err != nil {
251-
return nil, fmt.Errorf("copying elf file: %s", err)
246+
// Copy .elf file to sketch directory
247+
srcElf := outputPath.Parent().Join(base + ".elf")
248+
if srcElf.Exist() {
249+
dstElf := exportPath.Join(exportFile + ".elf")
250+
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
251+
if err = srcElf.CopyTo(dstElf); err != nil {
252+
return nil, fmt.Errorf("copying elf file: %s", err)
253+
}
252254
}
253255
}
254256

Diff for: rpc/commands/compile.pb.go

+36-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/commands/compile.proto

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ message CompileReq {
3838
int32 jobs = 14; // The max number of concurrent compiler instances to run (as make -jx)
3939
repeated string libraries = 15; // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
4040
bool optimizeForDebug = 16; // Optimize compile output for debug, not for release
41+
bool dryRun = 17; // When set to true the compiled binary will not be copied to the export directory
4142
}
4243

4344
message CompileResp {

0 commit comments

Comments
 (0)